mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-22 00:25:43 +00:00
automatically detect image types and set primary photo if first
This commit is contained in:
parent
3f455cb574
commit
51e3ad11c4
4 changed files with 39 additions and 9 deletions
|
@ -3,6 +3,7 @@ package v1
|
|||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/internal/core/services"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/ent/attachment"
|
||||
|
@ -67,7 +68,15 @@ func (ctrl *V1Controller) HandleItemAttachmentCreate() errchain.HandlerFunc {
|
|||
|
||||
attachmentType := r.FormValue("type")
|
||||
if attachmentType == "" {
|
||||
attachmentType = attachment.TypeAttachment.String()
|
||||
// Attempt to auto-detect the type of the file
|
||||
ext := filepath.Ext(attachmentName)
|
||||
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".tiff":
|
||||
attachmentType = attachment.TypePhoto.String()
|
||||
default:
|
||||
attachmentType = attachment.TypeAttachment.String()
|
||||
}
|
||||
}
|
||||
|
||||
id, err := ctrl.routeID(r)
|
||||
|
|
|
@ -52,11 +52,30 @@ func ToItemAttachment(attachment *ent.Attachment) ItemAttachment {
|
|||
}
|
||||
|
||||
func (r *AttachmentRepo) Create(ctx context.Context, itemId, docId uuid.UUID, typ attachment.Type) (*ent.Attachment, error) {
|
||||
return r.db.Attachment.Create().
|
||||
bldr := r.db.Attachment.Create().
|
||||
SetType(typ).
|
||||
SetDocumentID(docId).
|
||||
SetItemID(itemId).
|
||||
Save(ctx)
|
||||
SetItemID(itemId)
|
||||
|
||||
// Autoset primary to true if this is the first attachment
|
||||
// that is of type photo
|
||||
if typ == attachment.TypePhoto {
|
||||
cnt, err := r.db.Attachment.Query().
|
||||
Where(
|
||||
attachment.HasItemWith(item.ID(itemId)),
|
||||
attachment.TypeEQ(typ),
|
||||
).
|
||||
Count(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cnt == 0 {
|
||||
bldr = bldr.SetPrimary(true)
|
||||
}
|
||||
}
|
||||
|
||||
return bldr.Save(ctx)
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) Get(ctx context.Context, id uuid.UUID) (*ent.Attachment, error) {
|
||||
|
@ -75,7 +94,7 @@ func (r *AttachmentRepo) Update(ctx context.Context, itemId uuid.UUID, data *Ite
|
|||
bldr := r.db.Attachment.UpdateOneID(itemId).
|
||||
SetType(typ)
|
||||
|
||||
// Primary only applies to photos
|
||||
// Primary only applies to photos
|
||||
if typ == attachment.TypePhoto {
|
||||
bldr = bldr.SetPrimary(data.Primary)
|
||||
} else {
|
||||
|
|
|
@ -28,10 +28,12 @@ export type ItemsQuery = {
|
|||
};
|
||||
|
||||
export class AttachmentsAPI extends BaseAPI {
|
||||
add(id: string, file: File | Blob, filename: string, type: AttachmentTypes) {
|
||||
add(id: string, file: File | Blob, filename: string, type: AttachmentTypes | null = null) {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("type", type);
|
||||
if (type) {
|
||||
formData.append("type", type);
|
||||
}
|
||||
formData.append("name", filename);
|
||||
|
||||
return this.http.post<FormData, ItemOut>({
|
||||
|
|
|
@ -253,7 +253,7 @@
|
|||
return;
|
||||
}
|
||||
|
||||
uploadAttachment([first], AttachmentTypes.Attachment);
|
||||
uploadAttachment([first], null);
|
||||
}
|
||||
|
||||
const dropPhoto = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Photo);
|
||||
|
@ -262,7 +262,7 @@
|
|||
const dropManual = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Manual);
|
||||
const dropReceipt = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Receipt);
|
||||
|
||||
async function uploadAttachment(files: File[] | null, type: AttachmentTypes) {
|
||||
async function uploadAttachment(files: File[] | null, type: AttachmentTypes | null) {
|
||||
if (!files || files.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue