feat: improve ArtCraft integration and configurable file opening

This commit is contained in:
SantaSpeen committed 2026-11-02 00:37:44 +03:00
1 parent 3bc62d2b69
commit 80bc2c458d
60 files changed
+1738 -232

No files matched your search

+24 -8
View File
@@ -20,7 +20,7 @@ import (
"github.com/spf13/afero"
)
const maxCraftDocument = 64 << 20
const maxCraftDocument = 256 << 20
// Normal uploads/moves take a shared lock; editor commit/read takes the exclusive
// lock so the version check and replacement cannot race our own file API.
@@ -33,8 +33,11 @@ func readCraftFile(d *data, name string) ([]byte, os.FileInfo, error) {
if err != nil {
return nil, nil, err
}
if !info.Mode().IsRegular() || info.Size() > maxCraftDocument {
return nil, nil, errors.New("unsupported document size or type")
if !info.Mode().IsRegular() {
return nil, nil, os.ErrInvalid
}
if info.Size() > maxCraftDocument {
return nil, nil, errCraftDocumentSize
}
f, err := d.user.Fs.Open(name)
if err != nil {
@@ -43,10 +46,13 @@ func readCraftFile(d *data, name string) ([]byte, os.FileInfo, error) {
defer f.Close()
data, err := io.ReadAll(io.LimitReader(f, maxCraftDocument+1))
if len(data) > maxCraftDocument {
return nil, nil, errors.New("document exceeds 64 MiB")
return nil, nil, errCraftDocumentSize
}
return data, info, err
}
var errCraftDocumentSize = errors.New("file is too large: editors support files up to 256 MiB")
func craftDocumentHandler(manager *addons.Manager, cache FileCache) handleFunc {
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
id := mux.Vars(r)["addon"]
@@ -54,7 +60,8 @@ func craftDocumentHandler(manager *addons.Manager, cache FileCache) handleFunc {
if !ok {
return http.StatusNotFound, nil
}
if !addonEnabled(d, id) || manager.Status(id).Build == "" || !d.user.Perm.Download || !d.user.Perm.Modify {
status := manager.Status(id)
if !addonEnabled(d, id) || status.Build == "" || addons.CompatibilityIssue(id, status.Build) != "" || !d.user.Perm.Download || !d.user.Perm.Modify {
return http.StatusForbidden, nil
}
requested := r.URL.Query().Get("path")
@@ -62,7 +69,7 @@ func craftDocumentHandler(manager *addons.Manager, cache FileCache) handleFunc {
return http.StatusBadRequest, nil
}
extension := strings.ToLower(path.Ext(requested))
if (r.Method == "PUT" && !tool.CanSave(extension)) || (r.Method != "PUT" && !tool.CanOpen(extension)) {
if (r.Method == "PUT" && !tool.CanSave(extension)) || (r.Method == "POST" && !tool.CanOpen(extension)) || (r.Method == "GET" && !tool.CanOpen(extension) && !tool.CanSave(extension)) {
return http.StatusUnsupportedMediaType, nil
}
if !d.Check(requested) {
@@ -79,6 +86,12 @@ func craftDocumentHandler(manager *addons.Manager, cache FileCache) handleFunc {
data, _, err := readCraftFile(d, requested)
craftMutationMu.Unlock()
if err != nil {
if errors.Is(err, os.ErrInvalid) {
return http.StatusBadRequest, err
}
if errors.Is(err, errCraftDocumentSize) {
return http.StatusRequestEntityTooLarge, err
}
return errToStatus(err), err
}
if err = validateCraftDocument(extension, data); err != nil {
@@ -117,14 +130,17 @@ func craftDocumentHandler(manager *addons.Manager, cache FileCache) handleFunc {
defer d.user.Fs.Remove(tempName)
n, err := io.Copy(temp, io.LimitReader(body, maxCraftDocument+1))
if err == nil && n > maxCraftDocument {
err = errors.New("document exceeds 64 MiB")
err = errCraftDocumentSize
}
if err == nil {
err = temp.Sync()
}
if err != nil {
temp.Close()
return http.StatusRequestEntityTooLarge, err
if errors.Is(err, errCraftDocumentSize) {
return http.StatusRequestEntityTooLarge, err
}
return errToStatus(err), err
}
if _, err = temp.Seek(0, io.SeekStart); err != nil {
temp.Close()