1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00
mycorrhiza/shroom/upload.go
2021-07-02 15:29:55 +07:00

101 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package shroom
import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
"strings"
"github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/mimetype"
"github.com/bouncepaw/mycorrhiza/user"
)
func UploadText(h *hyphae.Hypha, data []byte, message string, u *user.User) (hop *history.HistoryOp, errtitle string) {
hop = history.Operation(history.TypeEditText)
var action string
if h.Exists {
action = "Edit"
} else {
action = "Create"
}
if message == "" {
hop.WithMsg(fmt.Sprintf("%s %s", action, h.Name))
} else {
hop.WithMsg(fmt.Sprintf("%s %s: %s", action, h.Name, message))
}
if err, errtitle := CanEdit(u, h); err != nil {
return hop.WithErrAbort(err), errtitle
}
if len(data) == 0 {
return hop.WithErrAbort(errors.New("No data passed")), "Empty"
}
return uploadHelp(h, hop, ".myco", data, u)
}
func UploadBinary(h *hyphae.Hypha, mime string, file multipart.File, u *user.User) (*history.HistoryOp, string) {
var (
hop = history.Operation(history.TypeEditBinary).WithMsg(fmt.Sprintf("Upload binary part for %s with type %s", h.Name, mime))
data, err = io.ReadAll(file)
)
if err != nil {
return hop.WithErrAbort(err), err.Error()
}
if err, errtitle := CanAttach(u, h); err != nil {
return hop.WithErrAbort(err), errtitle
}
if len(data) == 0 {
return hop.WithErrAbort(errors.New("No data passed")), "Empty"
}
return uploadHelp(h, hop, mimetype.ToExtension(mime), data, u)
}
// uploadHelp is a helper function for UploadText and UploadBinary
func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte, u *user.User) (*history.HistoryOp, string) {
var (
fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext)
originalFullPath = &h.TextPath
)
// Reject if the path is outside the hyphae dir
if !strings.HasPrefix(fullPath, files.HyphaeDir()) {
err := errors.New("bad path")
return hop.WithErrAbort(err), err.Error()
}
if hop.Type == history.TypeEditBinary {
originalFullPath = &h.BinaryPath
}
if err := os.MkdirAll(filepath.Dir(fullPath), 0777); err != nil {
return hop.WithErrAbort(err), err.Error()
}
if err := os.WriteFile(fullPath, data, 0666); err != nil {
return hop.WithErrAbort(err), err.Error()
}
if h.Exists && *originalFullPath != fullPath && *originalFullPath != "" {
if err := history.Rename(*originalFullPath, fullPath); err != nil {
return hop.WithErrAbort(err), err.Error()
}
log.Println("Move", *originalFullPath, "to", fullPath)
}
h.InsertIfNew()
if h.Exists && h.TextPath != "" && hop.Type == history.TypeEditText && !history.FileChanged(fullPath) {
return hop.Abort(), "No changes"
}
*originalFullPath = fullPath
return hop.WithFiles(fullPath).WithUser(u).Apply(), ""
}