From 51e3ad11c429bab8b3b158c751fd1e6f470654bb Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Fri, 1 Dec 2023 11:45:20 -0600 Subject: [PATCH] automatically detect image types and set primary photo if first --- .../handlers/v1/v1_ctrl_items_attachments.go | 11 +++++++- .../data/repo/repo_item_attachments.go | 27 ++++++++++++++++--- frontend/lib/api/classes/items.ts | 6 +++-- frontend/pages/item/[id]/index/edit.vue | 4 +-- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go index eefc28e..12bbbb5 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go @@ -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) diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.go index a015e71..1335f44 100644 --- a/backend/internal/data/repo/repo_item_attachments.go +++ b/backend/internal/data/repo/repo_item_attachments.go @@ -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 { diff --git a/frontend/lib/api/classes/items.ts b/frontend/lib/api/classes/items.ts index fc0ac57..a8c00e7 100644 --- a/frontend/lib/api/classes/items.ts +++ b/frontend/lib/api/classes/items.ts @@ -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({ diff --git a/frontend/pages/item/[id]/index/edit.vue b/frontend/pages/item/[id]/index/edit.vue index 73248a2..73b8c51 100644 --- a/frontend/pages/item/[id]/index/edit.vue +++ b/frontend/pages/item/[id]/index/edit.vue @@ -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; }