<commit_before>package browse

import (
	"errors"
	"github.com/scritch007/ShareMinatorApiGenerator/api"
	"github.com/scritch007/go-tools"
	"github.com/scritch007/shareit/auth"
	"github.com/scritch007/shareit/thumbnail"
	"github.com/scritch007/shareit/types"
	"io/ioutil"
	"mime"
	"net/http"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"time"
)

type BrowseHandler struct {
	config    *types.Configuration
	thumbnail *thumbnail.ThumbnailGenerator
}

func NewBrowseHandler(config *types.Configuration) (handler *BrowseHandler) {
	handler = &BrowseHandler{config: config, thumbnail: thumbnail.NewThumbnailGenerator(config)}
	return handler
}

func (b *BrowseHandler) Handle(context *types.CommandContext, resp chan<- types.EnumCommandHandlerStatus) *types.HttpError {
	command := context.Command
	tools.LOG_DEBUG.Println("Got this command ", command.ApiCommand.Name)
	if nil == command.ApiCommand.Browser {
		return &types.HttpError{Err: errors.New("Missing browse command body"), Status: http.StatusBadRequest}
	}
	if command.ApiCommand.Name == api.EnumBrowserList {
		go b.browseCommand(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserCreateFolder {
		go b.createFolderCommand(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserDelete {
		go b.deleteItemCommand(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserDownloadLink {
		go b.downloadLink(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserUploadFile {
		go b.uploadFile(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserThumbnail {
		go b.thumbnailCommand(context, resp)
	} else {
		return &types.HttpError{Err: errors.New("Unknown Browse command"), Status: http.StatusBadRequest}
	}
	return nil
}

func (b *BrowseHandler) downloadLink(context *types.CommandContext, resp chan<- types.EnumCommandHandlerStatus) {
	command := context.Command.ApiCommand
	if nil == command.Browser.DownloadLink {
		tools.LOG_DEBUG.Println("Missing input configuration")
		command.State.ErrorCode = api.ERROR_MISSING_COMMAND_BODY
		resp <- types.EnumCommandHandlerError
		return
	}

	accessPath, _ := auth.GetAccessAndPath(b.config, context, command.Browser.DownloadLink.Input.Path, false)
	if !accessPath.Exists {
		tools.LOG_DEBUG.Println("Couldn't find this path")
		command.State.ErrorCode = api.ERROR_INVALID_PATH
		resp <- types.EnumCommandHandlerError
		return
	}

	file_path := accessPath.RealPath
	result := tools.ComputeHmac256(*file_path, b.config.PrivateKey)
	dLink := types.DownloadLink{Link: result, Path: command.Browser.DownloadLink.Input.Path, RealPath: file_path}
	b.config.Db.AddDownloadLink(&dLink)
	command.Browser.DownloadLink.Output.DownloadLink = url.QueryEscape(result)
	resp <- types.EnumCommandHandlerDone
}

//Handle removal of an item
func (b *BrowseHandler) deleteItemCommand(context *types.CommandContext, resp chan<- types.EnumCommandHandlerStatus) {
	command := context.Command.ApiCommand
	if nil == command.Browser.Delete {
		tools.LOG_DEBUG.Println("Missing input configuration")
		command.State.ErrorCode = api.ERROR_MISSING_COMMAND_BODY
		resp <- types.EnumCommandHandlerError<commit_msg>Fix issue with generated download links<commit_after>package browse

import (
	"errors"
	"github.com/scritch007/ShareMinatorApiGenerator/api"
	"github.com/scritch007/go-tools"
	"github.com/scritch007/shareit/auth"
	"github.com/scritch007/shareit/thumbnail"
	"github.com/scritch007/shareit/types"
	"io/ioutil"
	"mime"
	"net/http"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"time"
	"strings"
)

type BrowseHandler struct {
	config    *types.Configuration
	thumbnail *thumbnail.ThumbnailGenerator
}

func NewBrowseHandler(config *types.Configuration) (handler *BrowseHandler) {
	handler = &BrowseHandler{config: config, thumbnail: thumbnail.NewThumbnailGenerator(config)}
	return handler
}

func (b *BrowseHandler) Handle(context *types.CommandContext, resp chan<- types.EnumCommandHandlerStatus) *types.HttpError {
	command := context.Command
	tools.LOG_DEBUG.Println("Got this command ", command.ApiCommand.Name)
	if nil == command.ApiCommand.Browser {
		return &types.HttpError{Err: errors.New("Missing browse command body"), Status: http.StatusBadRequest}
	}
	if command.ApiCommand.Name == api.EnumBrowserList {
		go b.browseCommand(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserCreateFolder {
		go b.createFolderCommand(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserDelete {
		go b.deleteItemCommand(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserDownloadLink {
		go b.downloadLink(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserUploadFile {
		go b.uploadFile(context, resp)
	} else if command.ApiCommand.Name == api.EnumBrowserThumbnail {
		go b.thumbnailCommand(context, resp)
	} else {
		return &types.HttpError{Err: errors.New("Unknown Browse command"), Status: http.StatusBadRequest}
	}
	return nil
}

func (b *BrowseHandler) downloadLink(context *types.CommandContext, resp chan<- types.EnumCommandHandlerStatus) {
	command := context.Command.ApiCommand
	if nil == command.Browser.DownloadLink {
		tools.LOG_DEBUG.Println("Missing input configuration")
		command.State.ErrorCode = api.ERROR_MISSING_COMMAND_BODY
		resp <- types.EnumCommandHandlerError
		return
	}

	accessPath, _ := auth.GetAccessAndPath(b.config, context, command.Browser.DownloadLink.Input.Path, false)
	if !accessPath.Exists {
		tools.LOG_DEBUG.Println("Couldn't find this path")
		command.State.ErrorCode = api.ERROR_INVALID_PATH
		resp <- types.EnumCommandHandlerError
		return
	}

	file_path := accessPath.RealPath
	result := tools.ComputeHmac256(*file_path, b.config.PrivateKey)
	strings.Replace(result, "/", "_", -1)
	dLink := types.DownloadLink{Link: result, Path: command.Browser.DownloadLink.Input.Path, RealPath: file_path}
	b.config.Db.AddDownloadLink(&dLink)
	command.Browser.DownloadLink.Output.DownloadLink = url.QueryEscape(result)
	resp <- types.EnumCommandHandlerDone
}

//Handle removal of an item
func (b *BrowseHandler) deleteItemCommand(context *types.CommandContext, resp chan<- types.EnumCommandHandlerStatus) {
	command := context.Command.ApiCommand
	if nil == command.Browser.Delete {
		tools.LOG_DEBUG.Println("Missing input configuration")
		command.State.ErrorCode = api.ERROR_MISSING_COMMAND_BODY
		resp <- types.EnumCommandHandlerError
{'fraction_non_alphanumeric': 0.11379310344827587, 'fraction_numerical': 0.005799373040752351, 'mean_word_length': 5.591942148760331, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '59915', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2039, 'n_words': 391}