Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
fix/feat: primary photo auto set and auto-set primary photo (#651)
Browse files Browse the repository at this point in the history
* fix error when item doesn't have photo attachment

* automatically detect image types and set primary photo if first

* remove charts.js from libs
  • Loading branch information
hay-kot authored Dec 1, 2023
1 parent d1d98bc commit 321a83b
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 424 deletions.
11 changes: 10 additions & 1 deletion backend/app/api/handlers/v1/v1_ctrl_items_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ require (
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down
70 changes: 1 addition & 69 deletions backend/go.sum

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions backend/internal/data/repo/repo_item_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions backend/internal/data/repo/repo_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ func (e *ItemsRepository) SetPrimaryPhotos(ctx context.Context, GID uuid.UUID) (
Where(
item.HasGroupWith(group.ID(GID)),
item.HasAttachmentsWith(
attachment.TypeEQ(attachment.TypePhoto),
attachment.Not(
attachment.And(
attachment.Primary(true),
Expand Down
94 changes: 0 additions & 94 deletions frontend/components/Chart/Donut.vue

This file was deleted.

113 changes: 0 additions & 113 deletions frontend/components/Chart/Line.vue

This file was deleted.

6 changes: 4 additions & 2 deletions frontend/lib/api/classes/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>({
Expand Down
2 changes: 0 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@vueuse/nuxt": "^10.0.0",
"@vueuse/router": "^10.0.0",
"autoprefixer": "^10.4.8",
"chart.js": "^4.0.1",
"daisyui": "^2.24.0",
"dompurify": "^3.0.0",
"h3": "^1.7.1",
Expand All @@ -54,7 +53,6 @@
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8",
"vue": "^3.3.1",
"vue-chartjs": "^4.1.2",
"vue-router": "4"
}
}
71 changes: 0 additions & 71 deletions frontend/pages/home/charts.ts

This file was deleted.

Loading

0 comments on commit 321a83b

Please sign in to comment.