feat: tags, unpublish and image upload for the author
- GET /_bm/api/v1/tags: the jz_bm_tags dictionary, to material managers
- POST /materials/{id}/unpublish: back to drafts, no revision bump, the
search document resynced
- POST /materials/{id}/images: one JPEG/PNG/GIF/WebP per request through
storeMaterialUpload, refusals with reason type, size or count
- storeMaterialUpload/fileRefusalFor: attach.ErrNotImage is a type
refusal, not a 500; storeMaterialFile keeps its texts
- the author's routes get their own per-user buckets (the inline
throttle form shares one counter per user)
- README: the author routes and revision on the student detail
This commit is contained in:
@@ -165,37 +165,65 @@ func (h *Handlers) storeParts(ctx context.Context, db *gorm.DB, bucket *blob.Buc
|
||||
return keys, err
|
||||
}
|
||||
|
||||
// storeMaterialFile stores one file through attach.Store as a public file of
|
||||
// m and binds it to the material, inside tx. It is the only way the
|
||||
// management API writes a material file: the multipart route, the inline
|
||||
// (base64) route and the signed upload all call it. The material's
|
||||
// MaterialFilesMax cap and attach's extension, MIME and size checks (lim)
|
||||
// apply. A refusal is returned as a text for the answer, with no error; the
|
||||
// returned file, when not nil, has blobs the caller deletes when tx does
|
||||
// not commit.
|
||||
func storeMaterialFile(ctx context.Context, tx *gorm.DB, bucket *blob.Bucket, m models.Material, name string, body io.Reader, lim attach.Limits) (*attach.File, string, error) {
|
||||
var have int64
|
||||
if err := materialFiles(tx.Model(&attach.File{}), m).Count(&have).Error; err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if have >= MaterialFilesMax {
|
||||
return nil, "A material holds at most " + strconv.Itoa(MaterialFilesMax) + " files.", nil
|
||||
}
|
||||
f, err := attach.Store(ctx, tx, bucket, attach.Upload{FileName: name, Body: body, Public: true}, lim)
|
||||
// fileRefusal is a file the material routes turn down, with no error:
|
||||
// Reason is "count" (the material holds MaterialFilesMax files), "size"
|
||||
// (attach.ErrTooLarge) or "type" (a type attach.Store refuses), and Text the
|
||||
// message of the answer. The zero value is no refusal.
|
||||
type fileRefusal struct {
|
||||
Reason string
|
||||
Text string
|
||||
}
|
||||
|
||||
// fileRefusalFor maps an attach.Store error to the refusal of file name
|
||||
// under lim, and reports false for an error that is not a refusal (nil
|
||||
// included). attach.ErrNotImage, the refusal of image mode, is a type
|
||||
// refusal, not a 500.
|
||||
func fileRefusalFor(err error, name string, lim attach.Limits) (fileRefusal, bool) {
|
||||
switch {
|
||||
case errors.Is(err, attach.ErrTooLarge):
|
||||
return nil, name + ": the file is larger than " + sizeMB(lim.MaxBytes) + ".", nil
|
||||
case errors.Is(err, attach.ErrFileType), errors.Is(err, attach.ErrMIMEType):
|
||||
return nil, name + ": this file type is not allowed.", nil
|
||||
case err != nil:
|
||||
return nil, "", err
|
||||
return fileRefusal{Reason: "size", Text: name + ": the file is larger than " + sizeMB(lim.MaxBytes) + "."}, true
|
||||
case errors.Is(err, attach.ErrFileType), errors.Is(err, attach.ErrMIMEType), errors.Is(err, attach.ErrNotImage):
|
||||
return fileRefusal{Reason: "type", Text: name + ": this file type is not allowed."}, true
|
||||
}
|
||||
return fileRefusal{}, false
|
||||
}
|
||||
|
||||
// storeMaterialUpload stores one file through attach.Store as a public file
|
||||
// of m and binds it to the material, inside tx. It is the only way the API
|
||||
// writes a material file: the multipart route, the inline (base64) route,
|
||||
// the signed upload and the editor image route all come through it. The
|
||||
// material's MaterialFilesMax cap and attach's extension, MIME, image and
|
||||
// size checks (lim) apply. A refusal is returned with no error; the
|
||||
// returned file, when not nil, has blobs the caller deletes when tx does
|
||||
// not commit.
|
||||
func storeMaterialUpload(ctx context.Context, tx *gorm.DB, bucket *blob.Bucket, m models.Material, name string, body io.Reader, lim attach.Limits) (*attach.File, fileRefusal, error) {
|
||||
var have int64
|
||||
if err := materialFiles(tx.Model(&attach.File{}), m).Count(&have).Error; err != nil {
|
||||
return nil, fileRefusal{}, err
|
||||
}
|
||||
if have >= MaterialFilesMax {
|
||||
return nil, fileRefusal{Reason: "count", Text: "A material holds at most " + strconv.Itoa(MaterialFilesMax) + " files."}, nil
|
||||
}
|
||||
f, err := attach.Store(ctx, tx, bucket, attach.Upload{FileName: name, Body: body, Public: true}, lim)
|
||||
if why, ok := fileRefusalFor(err, name, lim); ok {
|
||||
return nil, why, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fileRefusal{}, err
|
||||
}
|
||||
err = tx.Model(&attach.File{}).Where("id = ?", f.ID).Updates(map[string]any{
|
||||
"attachment_type": m.MorphName(),
|
||||
"attachment_id": strconv.FormatUint(uint64(m.ID), 10),
|
||||
"field": models.MaterialFilesField,
|
||||
}).Error
|
||||
return f, "", err
|
||||
return f, fileRefusal{}, err
|
||||
}
|
||||
|
||||
// storeMaterialFile is storeMaterialUpload with the refusal as its text,
|
||||
// for the management routes that answer only the text.
|
||||
func storeMaterialFile(ctx context.Context, tx *gorm.DB, bucket *blob.Bucket, m models.Material, name string, body io.Reader, lim attach.Limits) (*attach.File, string, error) {
|
||||
f, why, err := storeMaterialUpload(ctx, tx, bucket, m, name, body, lim)
|
||||
return f, why.Text, err
|
||||
}
|
||||
|
||||
// sizeMB names a byte limit in the refusal texts: 50 MB for 50 MiB, 3 MB
|
||||
|
||||
Reference in New Issue
Block a user