automatically detect image types and set primary photo if first

This commit is contained in:
Hayden 2023-12-01 11:45:20 -06:00
parent 3f455cb574
commit 51e3ad11c4
No known key found for this signature in database
GPG key ID: 17CF79474E257545
4 changed files with 39 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package v1
import ( import (
"errors" "errors"
"net/http" "net/http"
"path/filepath"
"github.com/hay-kot/homebox/backend/internal/core/services" "github.com/hay-kot/homebox/backend/internal/core/services"
"github.com/hay-kot/homebox/backend/internal/data/ent/attachment" "github.com/hay-kot/homebox/backend/internal/data/ent/attachment"
@ -67,7 +68,15 @@ func (ctrl *V1Controller) HandleItemAttachmentCreate() errchain.HandlerFunc {
attachmentType := r.FormValue("type") attachmentType := r.FormValue("type")
if attachmentType == "" { 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) id, err := ctrl.routeID(r)

View file

@ -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) { 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). SetType(typ).
SetDocumentID(docId). SetDocumentID(docId).
SetItemID(itemId). SetItemID(itemId)
Save(ctx)
// 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) { 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). bldr := r.db.Attachment.UpdateOneID(itemId).
SetType(typ) SetType(typ)
// Primary only applies to photos // Primary only applies to photos
if typ == attachment.TypePhoto { if typ == attachment.TypePhoto {
bldr = bldr.SetPrimary(data.Primary) bldr = bldr.SetPrimary(data.Primary)
} else { } else {

View file

@ -28,10 +28,12 @@ export type ItemsQuery = {
}; };
export class AttachmentsAPI extends BaseAPI { 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(); const formData = new FormData();
formData.append("file", file); formData.append("file", file);
formData.append("type", type); if (type) {
formData.append("type", type);
}
formData.append("name", filename); formData.append("name", filename);
return this.http.post<FormData, ItemOut>({ return this.http.post<FormData, ItemOut>({

View file

@ -253,7 +253,7 @@
return; return;
} }
uploadAttachment([first], AttachmentTypes.Attachment); uploadAttachment([first], null);
} }
const dropPhoto = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Photo); const dropPhoto = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Photo);
@ -262,7 +262,7 @@
const dropManual = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Manual); const dropManual = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Manual);
const dropReceipt = (files: File[] | null) => uploadAttachment(files, AttachmentTypes.Receipt); 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) { if (!files || files.length === 0) {
return; return;
} }