From 9a08a7334f1c4f609a666f3be75746b5adc0b950 Mon Sep 17 00:00:00 2001 From: Johnny Date: Mon, 9 Nov 2026 08:25:09 +0800 Subject: [PATCH] feat: add drawings with @usememos/canvas (#6533) --- core/usagelimit/usagelimit.go | 26 + core/usagelimit/usagelimit_test.go | 14 + internal/testutil/image.go | 20 +- proto/api/attachment_service.proto | 12 +- proto/gen/api/attachment_service.pb.go | 27 +- proto/gen/openapi.yaml | 10 +- server/api/api.go | 1 + server/api/attachment_service.go | 25 +- server/api/attachment_service_replace.go | 267 ++++++++++ server/api/attachment_service_replace_test.go | 234 +++++++++ server/api/attachment_upload.go | 34 +- server/api/usage_limit.go | 6 + server/fileserver/attachment_replace_test.go | 251 ++++++++++ server/fileserver/drawing_png_test.go | 127 +++++ server/fileserver/testdata/v1.canvas.png | Bin 0 -> 242 bytes store/attachment.go | 20 +- store/db/mysql/attachment.go | 17 +- store/db/postgres/attachment.go | 20 +- store/db/sqlite/attachment.go | 17 +- store/test/attachment_test.go | 50 ++ web/package.json | 3 +- web/pnpm-lock.yaml | 29 +- web/src/components/AppSidebar/TagsSection.tsx | 2 +- .../components/AppSidebar/sidebar-layout.ts | 3 +- .../components/DrawingSheet/DrawingFrame.tsx | 66 +++ .../components/DrawingSheet/DrawingSheet.tsx | 465 ++++++++++++++++++ .../components/DrawingSheet/DrawingViewer.tsx | 166 +++++++ web/src/components/DrawingSheet/drawing.ts | 28 ++ web/src/components/DrawingSheet/index.ts | 12 + .../components/DrawingSheet/useCanvasTheme.ts | 43 ++ .../MemoDetailSidebar/MemoDetailSidebar.tsx | 2 +- web/src/components/MemoEditor/README.md | 4 + .../MemoEditor/edit/EditSession.tsx | 25 +- .../MemoEditor/edit/Toolbar/EditorToolbar.tsx | 2 + .../MemoEditor/edit/Toolbar/InsertMenu.tsx | 15 +- .../edit/components/EditorMetadata.tsx | 4 + .../components/MemoEditor/edit/hooks/index.ts | 1 + .../MemoEditor/edit/hooks/useDrawing.ts | 121 +++++ .../MemoEditor/edit/hooks/useMemoSave.ts | 13 +- .../MemoEditor/edit/services/memoService.ts | 55 ++- .../edit/services/transcriptionService.ts | 5 +- .../MemoEditor/edit/services/uploadService.ts | 1 + .../MemoEditor/edit/types/attachment.ts | 8 + .../MemoEditor/edit/types/components.ts | 9 +- .../Attachment/AttachmentListEditor.tsx | 89 +++- .../Attachment/AttachmentListView.tsx | 148 +++++- .../Attachment/attachmentVisualClasses.ts | 17 + .../MemoView/components/MemoBody.tsx | 25 +- .../MemoView/components/MemoHeader.tsx | 2 +- web/src/components/UserMenu.tsx | 7 +- web/src/locales/ar.json | 41 ++ web/src/locales/az.json | 41 ++ web/src/locales/bg.json | 41 ++ web/src/locales/ca.json | 41 ++ web/src/locales/cs.json | 41 ++ web/src/locales/da.json | 41 ++ web/src/locales/de.json | 41 ++ web/src/locales/el.json | 41 ++ web/src/locales/en-GB.json | 41 ++ web/src/locales/en.json | 41 ++ web/src/locales/es.json | 41 ++ web/src/locales/et.json | 41 ++ web/src/locales/fa.json | 41 ++ web/src/locales/fi.json | 41 ++ web/src/locales/fr.json | 41 ++ web/src/locales/gl.json | 41 ++ web/src/locales/he.json | 41 ++ web/src/locales/hi.json | 41 ++ web/src/locales/hr.json | 41 ++ web/src/locales/hu.json | 41 ++ web/src/locales/id.json | 41 ++ web/src/locales/it.json | 41 ++ web/src/locales/ja.json | 41 ++ web/src/locales/ka-GE.json | 41 ++ web/src/locales/ko.json | 41 ++ web/src/locales/lt.json | 41 ++ web/src/locales/lv.json | 41 ++ web/src/locales/mr.json | 41 ++ web/src/locales/nb.json | 41 ++ web/src/locales/nl.json | 41 ++ web/src/locales/nn.json | 41 ++ web/src/locales/pl.json | 41 ++ web/src/locales/pt-BR.json | 41 ++ web/src/locales/pt-PT.json | 41 ++ web/src/locales/ro.json | 41 ++ web/src/locales/ru.json | 41 ++ web/src/locales/sk.json | 41 ++ web/src/locales/sl.json | 41 ++ web/src/locales/sr.json | 41 ++ web/src/locales/sv.json | 41 ++ web/src/locales/th.json | 41 ++ web/src/locales/tr.json | 41 ++ web/src/locales/uk.json | 41 ++ web/src/locales/vi.json | 41 ++ web/src/locales/zh-Hans.json | 41 ++ web/src/locales/zh-Hant.json | 41 ++ .../types/proto/api/attachment_service_pb.ts | 15 +- web/src/utils/attachment.ts | 10 + web/src/utils/user.ts | 3 + web/tests/attachment-replace.test.ts | 168 +++++++ web/tests/drawing-gallery.test.tsx | 68 +++ web/tests/drawing-sheet.test.tsx | 201 ++++++++ web/tests/drawing-viewer.test.tsx | 210 ++++++++ web/tests/drawing.test.tsx | 330 +++++++++++++ web/tests/fixtures/canvas/v1.canvas.png | Bin 0 -> 242 bytes web/tests/locale-resources.test.ts | 1 + web/tests/memo-body-compact.test.tsx | 14 +- web/tests/memo-editor-upload-retry.test.tsx | 18 +- 108 files changed, 5368 insertions(+), 104 deletions(-) create mode 100644 server/api/attachment_service_replace.go create mode 100644 server/api/attachment_service_replace_test.go create mode 100644 server/fileserver/attachment_replace_test.go create mode 100644 server/fileserver/drawing_png_test.go create mode 100644 server/fileserver/testdata/v1.canvas.png create mode 100644 web/src/components/DrawingSheet/DrawingFrame.tsx create mode 100644 web/src/components/DrawingSheet/DrawingSheet.tsx create mode 100644 web/src/components/DrawingSheet/DrawingViewer.tsx create mode 100644 web/src/components/DrawingSheet/drawing.ts create mode 100644 web/src/components/DrawingSheet/index.ts create mode 100644 web/src/components/DrawingSheet/useCanvasTheme.ts create mode 100644 web/src/components/MemoEditor/edit/hooks/useDrawing.ts create mode 100644 web/tests/attachment-replace.test.ts create mode 100644 web/tests/drawing-gallery.test.tsx create mode 100644 web/tests/drawing-sheet.test.tsx create mode 100644 web/tests/drawing-viewer.test.tsx create mode 100644 web/tests/drawing.test.tsx create mode 100644 web/tests/fixtures/canvas/v1.canvas.png diff --git a/core/usagelimit/usagelimit.go b/core/usagelimit/usagelimit.go index 68f14a1b1..c05fb3e76 100644 --- a/core/usagelimit/usagelimit.go +++ b/core/usagelimit/usagelimit.go @@ -177,6 +177,32 @@ func CheckUpload(ctx context.Context, s Store, userID int32, size int64) error { return nil } +// CheckReplace returns a *Refusal when replacing a stored file of replacedSize +// bytes with one of size bytes would break the per-file limit or the storage +// limit of userID. Only the difference counts toward storage, so a replacement +// no larger than the file it replaces is never refused for storage. +func CheckReplace(ctx context.Context, s Store, userID int32, size, replacedSize int64) error { + effective, err := EffectiveFor(ctx, s, userID) + if err != nil { + return err + } + if err := effective.checkFileSize(size); err != nil { + return err + } + limit := effective.Entry.GetStorageBytes() + if limit <= 0 || size <= replacedSize { + return nil + } + used, err := s.GetAttachmentStorageUsage(ctx, userID) + if err != nil { + return errors.Wrap(err, "failed to sum storage usage") + } + if used-replacedSize+size > limit { + return effective.refuse(MetricStorageBytes, limit, used) + } + return nil +} + // Usage is a user's usage and the limit set in effect for the user. type Usage struct { MemoCount int64 diff --git a/core/usagelimit/usagelimit_test.go b/core/usagelimit/usagelimit_test.go index 75a20deb2..5903df339 100644 --- a/core/usagelimit/usagelimit_test.go +++ b/core/usagelimit/usagelimit_test.go @@ -119,6 +119,20 @@ func TestCheckUpload(t *testing.T) { require.Zero(t, s.sumCalls, "no storage limit in the user's entry, so no sum query") } +func TestCheckReplace(t *testing.T) { + ctx := context.Background() + s := &fakeStore{storage: 900, setting: setting(&entry{AllUsers: true, StorageBytes: new(int64(1000)), UploadBytes: new(int64(300))})} + + require.NoError(t, CheckReplace(ctx, s, 1, 250, 150), "only the 100 extra bytes count: 900 - 150 + 250 = 1000") + require.Equal(t, &Refusal{Metric: MetricStorageBytes, Limit: 1000, Usage: 900, Index: 0}, AsRefusal(CheckReplace(ctx, s, 1, 251, 150))) + require.Equal(t, &Refusal{Metric: MetricUploadBytes, Limit: 300, Usage: 301, Index: 0}, AsRefusal(CheckReplace(ctx, s, 1, 301, 400))) + + s.sumCalls = 0 + s.storage = 5000 + require.NoError(t, CheckReplace(ctx, s, 1, 100, 200), "a smaller file is never refused for storage, even over the limit") + require.Zero(t, s.sumCalls, "a file no larger than the one it replaces needs no sum query") +} + func TestCheckFileSize(t *testing.T) { ctx := context.Background() s := &fakeStore{storage: 1 << 40, setting: setting(&entry{AllUsers: true, StorageBytes: new(int64(1)), UploadBytes: new(int64(200))})} diff --git a/internal/testutil/image.go b/internal/testutil/image.go index 320e1fa05..d087e5e4b 100644 --- a/internal/testutil/image.go +++ b/internal/testutil/image.go @@ -5,16 +5,32 @@ import ( "image" "image/color" "image/jpeg" + "image/png" ) -// BuildJPEG returns a solid-color width×height JPEG blob for tests. -func BuildJPEG(width, height int) []byte { +func solidImage(width, height int) *image.RGBA { img := image.NewRGBA(image.Rect(0, 0, width, height)) for y := range height { for x := range width { img.Set(x, y, color.RGBA{R: 80, G: 120, B: 160, A: 255}) } } + return img +} + +// BuildPNG returns a solid-color width×height PNG blob for tests. +func BuildPNG(width, height int) []byte { + var buffer bytes.Buffer + if err := png.Encode(&buffer, solidImage(width, height)); err != nil { + // Encoding an in-memory RGBA image cannot fail with valid dimensions. + panic(err) + } + return buffer.Bytes() +} + +// BuildJPEG returns a solid-color width×height JPEG blob for tests. +func BuildJPEG(width, height int) []byte { + img := solidImage(width, height) var buffer bytes.Buffer if err := jpeg.Encode(&buffer, img, &jpeg.Options{Quality: 90}); err != nil { // Encoding an in-memory RGBA image cannot fail with valid dimensions. diff --git a/proto/api/attachment_service.proto b/proto/api/attachment_service.proto index cf310d15e..3d3393c4f 100644 --- a/proto/api/attachment_service.proto +++ b/proto/api/attachment_service.proto @@ -243,7 +243,7 @@ message UploadAttachmentResponse { Attachment attachment = 4; } -// UploadAttachmentSpec describes the attachment that a new upload creates. +// UploadAttachmentSpec describes the attachment that a new upload creates, or whose content it replaces. message UploadAttachmentSpec { // Required. Metadata for the attachment to create. content must be empty; file bytes are sent in data. Attachment attachment = 1 [(google.api.field_behavior) = REQUIRED]; @@ -254,6 +254,16 @@ message UploadAttachmentSpec { // Optional. Total size of the file in bytes before media processing. Zero represents an empty file. int64 total_size = 3 [(google.api.field_behavior) = OPTIONAL]; + + // Optional. The name of an existing attachment whose content the finished upload replaces, instead of creating one. + // The attachment keeps its name, memo and creation time; its filename, type, size and media metadata come from this + // upload, and its cached thumbnail is dropped. attachment_id and attachment.memo must be empty. Only the uploader of + // the attachment or an admin may replace it. An external link or part of a motion photo cannot be replaced. + // Format: attachments/{attachment} + string replace_attachment = 4 [ + (google.api.field_behavior) = OPTIONAL, + (google.api.resource_reference) = {type: "memos.api/Attachment"} + ]; } // MotionMedia links the parts of a motion photo. diff --git a/proto/gen/api/attachment_service.pb.go b/proto/gen/api/attachment_service.pb.go index 8126f905b..06b47c299 100644 --- a/proto/gen/api/attachment_service.pb.go +++ b/proto/gen/api/attachment_service.pb.go @@ -853,7 +853,7 @@ func (x *UploadAttachmentResponse) GetAttachment() *Attachment { return nil } -// UploadAttachmentSpec describes the attachment that a new upload creates. +// UploadAttachmentSpec describes the attachment that a new upload creates, or whose content it replaces. type UploadAttachmentSpec struct { state protoimpl.MessageState `protogen:"open.v1"` // Required. Metadata for the attachment to create. content must be empty; file bytes are sent in data. @@ -862,9 +862,15 @@ type UploadAttachmentSpec struct { // Format: ^[a-zA-Z0-9]([a-zA-Z0-9-]{0,34}[a-zA-Z0-9])?$ AttachmentId string `protobuf:"bytes,2,opt,name=attachment_id,json=attachmentId,proto3" json:"attachment_id,omitempty"` // Optional. Total size of the file in bytes before media processing. Zero represents an empty file. - TotalSize int64 `protobuf:"varint,3,opt,name=total_size,json=totalSize,proto3" json:"total_size,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + TotalSize int64 `protobuf:"varint,3,opt,name=total_size,json=totalSize,proto3" json:"total_size,omitempty"` + // Optional. The name of an existing attachment whose content the finished upload replaces, instead of creating one. + // The attachment keeps its name, memo and creation time; its filename, type, size and media metadata come from this + // upload, and its cached thumbnail is dropped. attachment_id and attachment.memo must be empty. Only the uploader of + // the attachment or an admin may replace it. An external link or part of a motion photo cannot be replaced. + // Format: attachments/{attachment} + ReplaceAttachment string `protobuf:"bytes,4,opt,name=replace_attachment,json=replaceAttachment,proto3" json:"replace_attachment,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *UploadAttachmentSpec) Reset() { @@ -918,6 +924,13 @@ func (x *UploadAttachmentSpec) GetTotalSize() int64 { return 0 } +func (x *UploadAttachmentSpec) GetReplaceAttachment() string { + if x != nil { + return x.ReplaceAttachment + } + return "" +} + // MotionMedia links the parts of a motion photo. type MotionMedia struct { state protoimpl.MessageState `protogen:"open.v1"` @@ -1466,14 +1479,16 @@ const file_api_attachment_service_proto_rawDesc = "" + "\x0emax_chunk_size\x18\x03 \x01(\x05R\fmaxChunkSize\x125\n" + "\n" + "attachment\x18\x04 \x01(\v2\x15.memos.api.AttachmentR\n" + - "attachment\"\xa0\x01\n" + + "attachment\"\xed\x01\n" + "\x14UploadAttachmentSpec\x12:\n" + "\n" + "attachment\x18\x01 \x01(\v2\x15.memos.api.AttachmentB\x03\xe0A\x02R\n" + "attachment\x12(\n" + "\rattachment_id\x18\x02 \x01(\tB\x03\xe0A\x01R\fattachmentId\x12\"\n" + "\n" + - "total_size\x18\x03 \x01(\x03B\x03\xe0A\x01R\ttotalSize\"\xf8\x01\n" + + "total_size\x18\x03 \x01(\x03B\x03\xe0A\x01R\ttotalSize\x12K\n" + + "\x12replace_attachment\x18\x04 \x01(\tB\x1c\xe0A\x01\xfaA\x16\n" + + "\x14memos.api/AttachmentR\x11replaceAttachment\"\xf8\x01\n" + "\vMotionMedia\x124\n" + "\x06family\x18\x01 \x01(\x0e2\x1c.memos.api.MotionMediaFamilyR\x06family\x12.\n" + "\x04role\x18\x02 \x01(\x0e2\x1a.memos.api.MotionMediaRoleR\x04role\x12\x19\n" + diff --git a/proto/gen/openapi.yaml b/proto/gen/openapi.yaml index f67e68ceb..272e13a52 100644 --- a/proto/gen/openapi.yaml +++ b/proto/gen/openapi.yaml @@ -5403,7 +5403,15 @@ components: totalSize: type: string description: Optional. Total size of the file in bytes before media processing. Zero represents an empty file. - description: UploadAttachmentSpec describes the attachment that a new upload creates. + replaceAttachment: + type: string + description: |- + Optional. The name of an existing attachment whose content the finished upload replaces, instead of creating one. + The attachment keeps its name, memo and creation time; its filename, type, size and media metadata come from this + upload, and its cached thumbnail is dropped. attachment_id and attachment.memo must be empty. Only the uploader of + the attachment or an admin may replace it. An external link or part of a motion photo cannot be replaced. + Format: attachments/{attachment} + description: UploadAttachmentSpec describes the attachment that a new upload creates, or whose content it replaces. UpsertMemoReactionRequest: required: - parent diff --git a/server/api/api.go b/server/api/api.go index 00d65e2bd..1a952f66f 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -92,6 +92,7 @@ type APIService struct { linkMetadataFetcher linkMetadataFetcher attachmentUploads attachmentUploads + attachmentReplaces attachmentReplaceLocks memoImports memoImports } diff --git a/server/api/attachment_service.go b/server/api/attachment_service.go index a373c4dcf..834695572 100644 --- a/server/api/attachment_service.go +++ b/server/api/attachment_service.go @@ -191,10 +191,21 @@ type attachmentSource interface { // the content, and creates the database row. create.Size must hold the source // length on entry; it is updated when stripping re-encodes the image. func (s *APIService) processAndSaveAttachment(ctx context.Context, create *store.Attachment, instanceStorageSetting *storepb.InstanceStorageSetting, source attachmentSource) (*apipb.Attachment, error) { - if create.Payload.GetMotionMedia() == nil && (create.Type == "image/jpeg" || create.Type == "image/jpg") { + if err := s.processAndStoreContent(ctx, create, instanceStorageSetting, source, true); err != nil { + return nil, err + } + return s.persistAttachment(ctx, create, instanceStorageSetting) +} + +// processAndStoreContent strips EXIF metadata and writes the content to the +// default storage, filling in create's storage fields. With detectMotion, a +// JPEG that is an Android motion photo becomes a motion container and keeps +// its embedded video unstripped. It creates no database row. +func (s *APIService) processAndStoreContent(ctx context.Context, create *store.Attachment, instanceStorageSetting *storepb.InstanceStorageSetting, source attachmentSource, detectMotion bool) error { + if detectMotion && create.Payload.GetMotionMedia() == nil && (create.Type == "image/jpeg" || create.Type == "image/jpg") { detected, err := motionphoto.DetectJPEGReader(source, create.Size) if err != nil { - return nil, status.Errorf(codes.Internal, "failed to inspect motion photo: %v", err) + return status.Errorf(codes.Internal, "failed to inspect motion photo: %v", err) } if detected != nil { create.Payload = ensureAttachmentPayload(create.Payload) @@ -215,12 +226,12 @@ func (s *APIService) processAndSaveAttachment(ctx context.Context, create *store // client cannot skip stripping by labeling a JPEG as something else. needsExifStrip, err := shouldStripExifContent(source, create.Type) if err != nil { - return nil, status.Errorf(codes.Internal, "failed to inspect attachment content: %v", err) + return status.Errorf(codes.Internal, "failed to inspect attachment content: %v", err) } if needsExifStrip && !isAndroidMotionContainer(create.Payload.GetMotionMedia()) { release, err := s.acquireImageProcessingSlot(ctx) if err != nil { - return nil, status.Errorf(codes.ResourceExhausted, "too many image processing requests") + return status.Errorf(codes.ResourceExhausted, "too many image processing requests") } stripped, stripErr := stripImageExif(source, create.Type) release() @@ -237,12 +248,12 @@ func (s *APIService) processAndSaveAttachment(ctx context.Context, create *store } if _, err := content.Seek(0, io.SeekStart); err != nil { - return nil, status.Errorf(codes.Internal, "failed to rewind attachment content: %v", err) + return status.Errorf(codes.Internal, "failed to rewind attachment content: %v", err) } if err := saveAttachmentContent(ctx, s.Profile, s.Store, create, instanceStorageSetting, content); err != nil { - return nil, status.Errorf(codes.Internal, "failed to save attachment blob: %v", err) + return status.Errorf(codes.Internal, "failed to save attachment blob: %v", err) } - return s.persistAttachment(ctx, create, instanceStorageSetting) + return nil } func (s *APIService) persistAttachment(ctx context.Context, create *store.Attachment, instanceStorageSetting *storepb.InstanceStorageSetting) (*apipb.Attachment, error) { diff --git a/server/api/attachment_service_replace.go b/server/api/attachment_service_replace.go new file mode 100644 index 000000000..2f798abed --- /dev/null +++ b/server/api/attachment_service_replace.go @@ -0,0 +1,267 @@ +package api + +import ( + "context" + "io" + "log/slog" + "os" + "sync" + "time" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" + + "github.com/usememos/memos/core/access" + apipb "github.com/usememos/memos/proto/gen/api" + storepb "github.com/usememos/memos/proto/gen/store" + "github.com/usememos/memos/store" +) + +// Replacing an attachment's content (UploadAttachmentSpec.replace_attachment) +// keeps the attachment's identity: its name, memo binding, creator and +// creation time. The new file is written as a separate storage object first, +// the row is pointed at it in one update, and only then is the previous object +// deleted, so a failure at any step leaves one complete file behind. Thumbnails +// and other derived files are keyed by the attachment UID and are dropped with +// the previous object. + +// attachmentReplaceLocks serializes replacements of one attachment within the +// process, so each one deletes exactly the object the previous one left. Locks +// are reference counted and dropped when no replacement of the UID is running. +type attachmentReplaceLocks struct { + mu sync.Mutex + locks map[string]*attachmentReplaceLock +} + +type attachmentReplaceLock struct { + sync.Mutex + holders int +} + +// lock blocks until no other replacement of uid runs, and returns its release. +func (l *attachmentReplaceLocks) lock(uid string) func() { + l.mu.Lock() + if l.locks == nil { + l.locks = map[string]*attachmentReplaceLock{} + } + entry := l.locks[uid] + if entry == nil { + entry = &attachmentReplaceLock{} + l.locks[uid] = entry + } + entry.holders++ + l.mu.Unlock() + + entry.Lock() + return func() { + entry.Unlock() + l.mu.Lock() + if entry.holders--; entry.holders == 0 { + delete(l.locks, uid) + } + l.mu.Unlock() + } +} + +// validateReplaceSpec checks the parts of an upload spec that a replacement +// fixes: the identity and the memo binding come from the replaced attachment. +func validateReplaceSpec(spec *apipb.UploadAttachmentSpec) error { + if spec.AttachmentId != "" { + return status.Errorf(codes.InvalidArgument, "attachment_id must be empty when replacing an attachment") + } + if spec.Attachment.GetMemo() != "" { + return status.Errorf(codes.InvalidArgument, "attachment.memo must be empty when replacing an attachment") + } + if spec.Attachment.GetMotionMedia() != nil { + return status.Errorf(codes.InvalidArgument, "a replacement cannot be part of a motion photo") + } + return nil +} + +// findReplaceableAttachment loads the attachment a replacement targets and +// checks that the current user may replace its content. +func (s *APIService) findReplaceableAttachment(ctx context.Context, name string) (*store.Attachment, *store.User, error) { + uid, err := ExtractAttachmentUIDFromName(name) + if err != nil { + return nil, nil, status.Errorf(codes.InvalidArgument, "invalid replace_attachment: %v", err) + } + user, err := s.fetchCurrentUser(ctx) + if err != nil { + return nil, nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) + } + if user == nil { + return nil, nil, status.Errorf(codes.Unauthenticated, "user not authenticated") + } + attachment, err := s.Store.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + if err != nil { + return nil, nil, status.Errorf(codes.Internal, "failed to get attachment: %v", err) + } + if attachment == nil { + return nil, nil, status.Errorf(codes.NotFound, "attachment not found") + } + if !access.CanManageAttachment(user, attachment) { + return nil, nil, status.Errorf(codes.PermissionDenied, "permission denied") + } + if attachment.StorageType == storepb.AttachmentStorageType_EXTERNAL { + return nil, nil, status.Errorf(codes.FailedPrecondition, "an external link has no stored content to replace") + } + if attachment.Payload.GetMotionMedia() != nil { + return nil, nil, status.Errorf(codes.FailedPrecondition, "part of a motion photo cannot be replaced") + } + return attachment, user, nil +} + +// finishAttachmentReplace stores a finished upload as the new content of the +// attachment it replaces. +func (s *APIService) finishAttachmentReplace(ctx context.Context, upload *attachmentUpload) (*apipb.Attachment, error) { + release := s.attachmentReplaces.lock(upload.state.uid) + defer release() + if upload.state.replaceCommitted { + // An earlier finish committed but failed afterwards: report the attachment as + // it is now, instead of applying this upload again. The previous file's + // cleanup does not decide the result; when it fails, the next retry tries again. + if leftover := upload.state.replacedLeftover; leftover != nil && s.deleteReplacedContent(ctx, leftover) { + upload.state.replacedLeftover = nil + } + return s.GetAttachment(ctx, &apipb.GetAttachmentRequest{Name: "attachments/" + upload.state.uid}) + } + // Check again at finish: the attachment may have been deleted, linked to a + // memo the user can no longer write, or the limits may have changed. + replaced, user, err := s.findReplaceableAttachment(ctx, "attachments/"+upload.state.uid) + if err != nil { + return nil, err + } + if err := s.checkReplaceQuota(ctx, replaced.CreatorID, upload.totalSize, replaced.Size); err != nil { + return nil, err + } + setting, err := s.Store.GetInstanceStorageSetting(ctx) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get storage setting: %v", err) + } + file, err := os.Open(upload.path) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to open upload file: %v", err) + } + defer file.Close() + metadata := proto.CloneOf(upload.state.metadata) + metadata.Content = make([]byte, min(upload.totalSize, 512)) + if _, err := io.ReadFull(file, metadata.Content); err != nil { + return nil, status.Errorf(codes.Internal, "failed to read attachment header: %v", err) + } + // Validate the filename, type and media metadata with the actual MIME type. + // The prepared attachment carries a fresh UID, which names the new storage + // object apart from the one it replaces until the row points at it. + content, err := s.prepareAttachment(ctx, &apipb.CreateAttachmentRequest{Attachment: metadata}) + if err != nil { + return nil, err + } + content.Size = upload.totalSize + upload.state.finalizeAttempted = true + // A replacement is never part of a motion photo: a motion JPEG is stored as + // a plain, EXIF-stripped image. + if err := s.processAndStoreContent(ctx, content, setting, file, false); err != nil { + return nil, err + } + // From here the new object is the attachment's, under the replaced UID. + content.UID = replaced.UID + // discardContent removes the new object when no row points at it. It checks + // the row first, so an update that committed before failing keeps its object. + discardContent := func(reason string) { + if _, err := cleanupSavedAttachmentBlob(ctx, s.Store, content, setting); err != nil { + slog.Error("failed to remove replacement content "+reason, + slog.String("attachment_uid", replaced.UID), + slog.Any("error", err), + ) + } + } + payload := content.Payload + if payload == nil { + // The previous payload described the previous object; none of it carries over. + payload = &storepb.AttachmentPayload{} + } + + nowSec := time.Now().Unix() + if err := s.Store.UpdateAttachment(ctx, &store.UpdateAttachment{ + ID: replaced.ID, + UpdatedTs: &nowSec, + Filename: &content.Filename, + Payload: payload, + Content: &store.AttachmentContent{ + Type: content.Type, + Size: content.Size, + StorageType: content.StorageType, + Reference: content.Reference, + Blob: content.Blob, + }, + Policy: memoWritePolicy(user.ID, false), + }); err != nil { + discardContent("after the update failed") + return nil, mapMemoWriteError(err, "failed to replace attachment content") + } + upload.state.replaceCommitted = true + + updated, err := s.Store.GetAttachment(ctx, &store.FindAttachment{UID: &replaced.UID}) + if err != nil { + // The replacement committed; the previous object is left in place + // rather than risk deleting the one the row now points at, and a retry + // deletes it once the row can be read. Its thumbnail is keyed by the + // UID, so it goes now. + upload.state.replacedLeftover = replaced + s.Store.DeleteAttachmentDerivedCaches(replaced) + s.SSEHub.publishMemoChanged() + return nil, status.Errorf(codes.Internal, "attachment content was replaced but failed to reload: %v", err) + } + if updated == nil { + // The attachment was deleted while its content was being replaced: the + // update matched no row, so nothing points at the new object. + discardContent("of a deleted attachment") + return nil, status.Errorf(codes.NotFound, "attachment not found") + } + if hasSameManagedStorageObject(replaced, updated) { + s.Store.DeleteAttachmentDerivedCaches(replaced) + } else if err := s.Store.DeleteAttachmentStorageWithInstanceSetting(ctx, replaced, setting); err != nil { + // The attachment is consistent; the previous file is only orphaned. + slog.Warn("failed to delete replaced attachment content", + slog.String("attachment_uid", replaced.UID), + slog.String("storage_type", replaced.StorageType.String()), + slog.Any("error", err), + ) + s.Store.DeleteAttachmentDerivedCaches(replaced) + } + if updated.MemoID != nil { + s.SSEHub.publishMemoChanged() + } + return convertAttachmentFromStore(updated), nil +} + +// deleteReplacedContent deletes the file a committed replacement left behind, +// unless the attachment's row still points at it (the replacement did not +// commit after all). A row that is gone means the attachment was deleted, and +// its file went with it. It reports whether the leftover needs no further +// cleanup; a failure is logged and leaves the leftover for the next retry. +func (s *APIService) deleteReplacedContent(ctx context.Context, leftover *store.Attachment) bool { + logFailure := func(msg string, err error) bool { + slog.Warn(msg, + slog.String("attachment_uid", leftover.UID), + slog.String("storage_type", leftover.StorageType.String()), + slog.Any("error", err), + ) + return false + } + current, err := s.Store.GetAttachment(ctx, &store.FindAttachment{UID: &leftover.UID}) + if err != nil { + return logFailure("failed to get attachment before deleting replaced content", err) + } + if current == nil || hasSameManagedStorageObject(current, leftover) { + return true + } + setting, err := s.Store.GetInstanceStorageSetting(ctx) + if err != nil { + return logFailure("failed to get storage setting before deleting replaced content", err) + } + if err := s.Store.DeleteAttachmentStorageWithInstanceSetting(ctx, leftover, setting); err != nil { + return logFailure("failed to delete replaced attachment content", err) + } + return true +} diff --git a/server/api/attachment_service_replace_test.go b/server/api/attachment_service_replace_test.go new file mode 100644 index 000000000..254f5631c --- /dev/null +++ b/server/api/attachment_service_replace_test.go @@ -0,0 +1,234 @@ +package api + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/usememos/memos/internal/testutil" + apipb "github.com/usememos/memos/proto/gen/api" + storepb "github.com/usememos/memos/proto/gen/store" + "github.com/usememos/memos/store" +) + +// A finish that committed a replacement but failed afterwards (the reload) can +// be retried by the client. The retry must report the attachment as it is, and +// never apply its upload again over a newer replacement. +func TestReplaceAttachmentRetryAfterCommitKeepsNewerReplacement(t *testing.T) { + svc := newIntegrationService(t) + t.Cleanup(svc.CloseUploads) + user := createSpaceTestUser(context.Background(), t, svc, "artist", store.RoleUser) + ctx := userCtx(context.Background(), user.ID) + attachment, err := svc.CreateAttachment(ctx, &apipb.CreateAttachmentRequest{Attachment: &apipb.Attachment{ + Filename: "drawing.canvas.png", Type: "image/png", Content: testutil.BuildPNG(4, 4), + }}) + require.NoError(t, err) + + replace := func(filename string, content []byte) *apipb.UploadAttachmentResponse { + response, err := svc.UploadAttachment(ctx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: filename, Type: "image/png"}, + TotalSize: int64(len(content)), + ReplaceAttachment: attachment.Name, + }}, + Data: content, + FinishWrite: true, + }) + require.NoError(t, err) + return response + } + first := testutil.BuildPNG(6, 6) + older := replace("drawing-older.canvas.png", first) + newer := replace("drawing-newer.canvas.png", testutil.BuildPNG(8, 8)) + + // Make the older upload look like its finish failed after the commit. + upload, err := svc.attachmentUploads.resume(older.UploadId, user.ID) + require.NoError(t, err) + upload.complete = false + upload.mu.Unlock() + + retried, err := svc.UploadAttachment(ctx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_UploadId{UploadId: older.UploadId}, + WriteOffset: int64(len(first)), + FinishWrite: true, + }) + require.NoError(t, err) + require.Equal(t, newer.Attachment.Filename, retried.Attachment.Filename, "the newer replacement stays") + require.Equal(t, newer.Attachment.Size, retried.Attachment.Size) +} + +// When the reload after a committed replacement fails, the previous file is +// kept for safety. The client's retry then deletes it, but never the file the +// attachment's row points at. +func TestReplaceAttachmentRetryDeletesLeftoverFile(t *testing.T) { + svc := newIntegrationService(t) + t.Cleanup(svc.CloseUploads) + ctx := context.Background() + _, err := svc.Store.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{ + Key: storepb.InstanceSettingKey_STORAGE, + Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{ + Storages: []*storepb.Storage{{Id: "local", Name: "Local", Type: storepb.StorageType_STORAGE_TYPE_LOCAL}}, + DefaultStorageId: "local", + }}, + }) + require.NoError(t, err) + user := createSpaceTestUser(ctx, t, svc, "artist", store.RoleUser) + userCtx := userCtx(ctx, user.ID) + content := testutil.BuildPNG(4, 4) + attachment, err := svc.CreateAttachment(userCtx, &apipb.CreateAttachmentRequest{Attachment: &apipb.Attachment{ + Filename: "drawing.canvas.png", Type: "image/png", Content: content, + }}) + require.NoError(t, err) + uid, err := ExtractAttachmentUIDFromName(attachment.Name) + require.NoError(t, err) + current, err := svc.Store.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + currentFile := localPath(svc.Profile.Data, current.Reference) + require.FileExists(t, currentFile) + + for _, tc := range []struct { + name string + leftover func() (*store.Attachment, string) + wantExists bool + }{ + { + name: "a file the row no longer points at is deleted", + leftover: func() (*store.Attachment, string) { + path := filepath.Join(filepath.Dir(currentFile), "previous.canvas.png") + require.NoError(t, os.WriteFile(path, content, 0o644)) + previous := *current + previous.Reference = path + return &previous, path + }, + }, + { + name: "the file the row points at stays", + leftover: func() (*store.Attachment, string) { + return current, currentFile + }, + wantExists: true, + }, + } { + t.Run(tc.name, func(t *testing.T) { + leftover, path := tc.leftover() + started, err := svc.UploadAttachment(userCtx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: "drawing.canvas.png", Type: "image/png"}, + TotalSize: int64(len(content)), + ReplaceAttachment: attachment.Name, + }}, + Data: content, + }) + require.NoError(t, err) + // The upload's first finish committed, then its reload failed. + upload, err := svc.attachmentUploads.resume(started.UploadId, user.ID) + require.NoError(t, err) + upload.state.replaceCommitted = true + upload.state.replacedLeftover = leftover + upload.mu.Unlock() + + retried, err := svc.UploadAttachment(userCtx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_UploadId{UploadId: started.UploadId}, + WriteOffset: int64(len(content)), + FinishWrite: true, + }) + require.NoError(t, err) + require.Equal(t, attachment.Name, retried.Attachment.Name) + if tc.wantExists { + require.FileExists(t, path) + } else { + require.NoFileExists(t, path) + } + require.FileExists(t, currentFile, "the attachment's own file is never deleted") + }) + } +} + +// A retry whose cleanup of the previous file fails still reports the committed +// replacement, and keeps the file queued so the next retry deletes it. +func TestReplaceAttachmentRetryKeepsLeftoverWhenDeleteFails(t *testing.T) { + svc := newIntegrationService(t) + t.Cleanup(svc.CloseUploads) + ctx := context.Background() + _, err := svc.Store.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{ + Key: storepb.InstanceSettingKey_STORAGE, + Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{ + Storages: []*storepb.Storage{{Id: "local", Name: "Local", Type: storepb.StorageType_STORAGE_TYPE_LOCAL}}, + DefaultStorageId: "local", + }}, + }) + require.NoError(t, err) + user := createSpaceTestUser(ctx, t, svc, "artist", store.RoleUser) + userCtx := userCtx(ctx, user.ID) + content := testutil.BuildPNG(4, 4) + attachment, err := svc.CreateAttachment(userCtx, &apipb.CreateAttachmentRequest{Attachment: &apipb.Attachment{ + Filename: "drawing.canvas.png", Type: "image/png", Content: content, + }}) + require.NoError(t, err) + uid, err := ExtractAttachmentUIDFromName(attachment.Name) + require.NoError(t, err) + current, err := svc.Store.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + + // A non-empty directory in place of the previous file: removing it fails. + stuck := filepath.Join(filepath.Dir(localPath(svc.Profile.Data, current.Reference)), "previous.canvas.png") + require.NoError(t, os.MkdirAll(stuck, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(stuck, "keep"), content, 0o644)) + leftover := *current + leftover.Reference = stuck + + started, err := svc.UploadAttachment(userCtx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: "drawing.canvas.png", Type: "image/png"}, + TotalSize: int64(len(content)), + ReplaceAttachment: attachment.Name, + }}, + Data: content, + }) + require.NoError(t, err) + upload, err := svc.attachmentUploads.resume(started.UploadId, user.ID) + require.NoError(t, err) + upload.state.replaceCommitted = true + upload.state.replacedLeftover = &leftover + upload.mu.Unlock() + + retry := func() { + upload, err := svc.attachmentUploads.resume(started.UploadId, user.ID) + require.NoError(t, err) + upload.complete = false + upload.mu.Unlock() + retried, err := svc.UploadAttachment(userCtx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_UploadId{UploadId: started.UploadId}, + WriteOffset: int64(len(content)), + FinishWrite: true, + }) + require.NoError(t, err, "a failed cleanup does not fail the committed replacement") + require.Equal(t, attachment.Name, retried.Attachment.Name) + } + pendingLeftover := func() *store.Attachment { + upload, err := svc.attachmentUploads.resume(started.UploadId, user.ID) + require.NoError(t, err) + defer upload.mu.Unlock() + return upload.state.replacedLeftover + } + + retry() + require.DirExists(t, stuck) + require.NotNil(t, pendingLeftover(), "the previous file stays queued for the next retry") + + require.NoError(t, os.Remove(filepath.Join(stuck, "keep"))) + retry() + require.NoDirExists(t, stuck) + require.Nil(t, pendingLeftover()) +} + +func localPath(data, reference string) string { + path := filepath.FromSlash(reference) + if filepath.IsAbs(path) { + return path + } + return filepath.Join(data, path) +} diff --git a/server/api/attachment_upload.go b/server/api/attachment_upload.go index c71ef875b..eb0b1895d 100644 --- a/server/api/attachment_upload.go +++ b/server/api/attachment_upload.go @@ -24,8 +24,17 @@ const ( // attachmentUploadState is what an attachment upload carries besides its // bytes. Only Attachment is an API resource. type attachmentUploadState struct { - metadata *apipb.Attachment - uid string + metadata *apipb.Attachment + uid string + // replace is set when the upload replaces the content of the existing + // attachment uid instead of creating it. + replace bool + // replaceCommitted is set once the replacement's row update committed, so + // a retried finish never applies this upload over a newer replacement. + replaceCommitted bool + // replacedLeftover is the attachment as it was before a committed + // replacement whose reload failed: its file still waits to be deleted. + replacedLeftover *store.Attachment finalizeAttempted bool } @@ -121,7 +130,20 @@ func (s *APIService) startAttachmentUpload(ctx context.Context, request *apipb.U if int64(len(request.Data)) > spec.TotalSize { return "", nil, status.Errorf(codes.InvalidArgument, "data exceeds total_size") } - if err := s.checkUploadQuota(ctx, ownerID, spec.TotalSize); err != nil { + var replaced *store.Attachment + if spec.ReplaceAttachment != "" { + if err := validateReplaceSpec(spec); err != nil { + return "", nil, err + } + target, _, err := s.findReplaceableAttachment(ctx, spec.ReplaceAttachment) + if err != nil { + return "", nil, err + } + if err := s.checkReplaceQuota(ctx, target.CreatorID, spec.TotalSize, target.Size); err != nil { + return "", nil, err + } + replaced = target + } else if err := s.checkUploadQuota(ctx, ownerID, spec.TotalSize); err != nil { return "", nil, err } // Validate before allocating a temporary file. Keep the original MIME type @@ -137,10 +159,16 @@ func (s *APIService) startAttachmentUpload(ctx context.Context, request *apipb.U return "", nil, err } state := attachmentUploadState{metadata: metadata, uid: create.UID} + if replaced != nil { + state.uid, state.replace = replaced.UID, true + } return s.attachmentUploads.create(s.Profile.Data, attachmentUploadTempPrefix, ownerID, spec.TotalSize, state) } func (s *APIService) finishAttachmentUpload(ctx context.Context, upload *attachmentUpload) (*apipb.Attachment, error) { + if upload.state.replace { + return s.finishAttachmentReplace(ctx, upload) + } if upload.state.finalizeAttempted { // A database operation may commit before returning an error. Resolve that // outcome before saving another object under the upload's stable UID. diff --git a/server/api/usage_limit.go b/server/api/usage_limit.go index 5276248dd..bc51eb848 100644 --- a/server/api/usage_limit.go +++ b/server/api/usage_limit.go @@ -26,6 +26,12 @@ func (s *APIService) checkUploadQuota(ctx context.Context, userID int32, size in return s.quotaError(ctx, userID, usagelimit.CheckUpload(ctx, s.Store, userID, size)) } +// checkReplaceQuota refuses replacing a stored file of replacedSize bytes with +// one of size bytes when it breaks a limit of userID, the file's owner. +func (s *APIService) checkReplaceQuota(ctx context.Context, userID int32, size, replacedSize int64) error { + return s.quotaError(ctx, userID, usagelimit.CheckReplace(ctx, s.Store, userID, size, replacedSize)) +} + // checkFileSizeQuota refuses a file of size bytes that is larger than the // per-file limit of the user. It runs no count or sum query. func (s *APIService) checkFileSizeQuota(ctx context.Context, userID int32, size int64) error { diff --git a/server/fileserver/attachment_replace_test.go b/server/fileserver/attachment_replace_test.go new file mode 100644 index 000000000..1800f7a8f --- /dev/null +++ b/server/fileserver/attachment_replace_test.go @@ -0,0 +1,251 @@ +package fileserver + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + "github.com/usememos/memos/internal/httphandler" + "github.com/usememos/memos/internal/testutil" + "github.com/usememos/memos/internal/testutil/fakes3" + apipb "github.com/usememos/memos/proto/gen/api" + storepb "github.com/usememos/memos/proto/gen/store" + "github.com/usememos/memos/server/api" + "github.com/usememos/memos/server/auth" + "github.com/usememos/memos/store" +) + +// replaceContent uploads content in one call that replaces the attachment's file. +func replaceContent(ctx context.Context, svc *api.APIService, name, filename string, content []byte) (*apipb.Attachment, error) { + response, err := svc.UploadAttachment(ctx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: filename, Type: "image/png"}, + TotalSize: int64(len(content)), + ReplaceAttachment: name, + }}, + Data: content, + FinishWrite: true, + }) + if err != nil { + return nil, err + } + return response.Attachment, nil +} + +// localFilePath resolves a LOCAL attachment reference the way the store does. +func localFilePath(data, reference string) string { + path := filepath.FromSlash(reference) + if filepath.IsAbs(path) { + return path + } + return filepath.Join(data, path) +} + +func TestReplaceAttachmentContent(t *testing.T) { + original, err := os.ReadFile("testdata/v1.canvas.png") + require.NoError(t, err) + const bucket = "file-server-attachments" + + for _, storageName := range []string{"database", "local", "s3"} { + t.Run(storageName, func(t *testing.T) { + ctx := context.Background() + svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + var fake *fakes3.Server + switch storageName { + case "database": + configureDatabaseStorage(ctx, t, stores) + case "local": + configureLocalStorage(ctx, t, stores) + case "s3": + fake = fakes3.New(t, bucket) + configureFakeS3Storage(ctx, t, stores, fake) + default: + t.Fatalf("unknown storage %q", storageName) + } + + owner, err := stores.CreateUser(ctx, &store.User{Username: "artist", Role: store.RoleUser, Email: "artist@example.com"}) + require.NoError(t, err) + ownerCtx := context.WithValue(ctx, auth.UserIDContextKey, owner.ID) + attachment := createDrawingInline(ownerCtx, t, svc, original) + memo, err := svc.CreateMemo(ownerCtx, &apipb.CreateMemoRequest{Memo: &apipb.Memo{ + Content: "a drawing", + Visibility: apipb.Visibility_PUBLIC, + Attachments: []*apipb.Attachment{{Name: attachment.Name}}, + }}) + require.NoError(t, err) + + uid, err := api.ExtractAttachmentUIDFromName(attachment.Name) + require.NoError(t, err) + before, err := stores.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + // A thumbnail the file server cached for the previous content. The + // store and the file server share one data directory in a real server. + thumbnail := filepath.Join(stores.GetDataDir(), ".thumbnail_cache", uid+".v2.jpeg") + require.NoError(t, os.MkdirAll(filepath.Dir(thumbnail), 0o755)) + require.NoError(t, os.WriteFile(thumbnail, []byte("stale"), 0o644)) + + edited := testutil.BuildPNG(8, 8) + replaced, err := replaceContent(ownerCtx, svc, attachment.Name, "drawing-20261106-130000.canvas.png", edited) + require.NoError(t, err) + + require.Equal(t, attachment.Name, replaced.Name, "the attachment keeps its name") + require.Equal(t, "drawing-20261106-130000.canvas.png", replaced.Filename) + require.Equal(t, int64(len(edited)), replaced.Size) + require.Equal(t, memo.Name, replaced.GetMemo(), "the attachment stays on its memo") + require.Equal(t, attachment.CreateTime.AsTime(), replaced.CreateTime.AsTime()) + require.NoFileExists(t, thumbnail, "the cached thumbnail of the previous content is dropped") + + mux := http.NewServeMux() + fs.RegisterRoutes(mux, http.HandlerFunc(httphandler.NotFound)) + recorder := httptest.NewRecorder() + mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/file/%s/%s", replaced.Name, replaced.Filename), nil)) + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, edited, recorder.Body.Bytes()) + + after, err := stores.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + require.Equal(t, before.ID, after.ID) + require.Equal(t, before.StorageType, after.StorageType) + + // The previous file is gone from its storage; the new one is there. + switch storageName { + case "local": + require.FileExists(t, localFilePath(svc.Profile.Data, after.Reference)) + require.NoFileExists(t, localFilePath(svc.Profile.Data, before.Reference)) + case "s3": + _, err := fake.GetObject(bucket, before.Payload.GetS3Object().GetKey()) + require.Error(t, err) + _, err = fake.GetObject(bucket, after.Payload.GetS3Object().GetKey()) + require.NoError(t, err) + default: + // A database blob is replaced in the row itself. + } + }) + } +} + +func TestReplaceAttachmentContentRefusals(t *testing.T) { + ctx := context.Background() + svc, _, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + content := testutil.BuildPNG(4, 4) + + owner, err := stores.CreateUser(ctx, &store.User{Username: "owner", Role: store.RoleUser, Email: "owner@example.com"}) + require.NoError(t, err) + other, err := stores.CreateUser(ctx, &store.User{Username: "other", Role: store.RoleUser, Email: "other@example.com"}) + require.NoError(t, err) + admin, err := stores.CreateUser(ctx, &store.User{Username: "admin", Role: store.RoleAdmin, Email: "admin@example.com"}) + require.NoError(t, err) + ownerCtx := context.WithValue(ctx, auth.UserIDContextKey, owner.ID) + otherCtx := context.WithValue(ctx, auth.UserIDContextKey, other.ID) + adminCtx := context.WithValue(ctx, auth.UserIDContextKey, admin.ID) + attachment := createDrawingInline(ownerCtx, t, svc, content) + + _, err = replaceContent(otherCtx, svc, attachment.Name, "drawing.canvas.png", content) + require.Equal(t, codes.PermissionDenied, status.Code(err), "only the uploader or an admin may replace it") + + _, err = replaceContent(ctx, svc, attachment.Name, "drawing.canvas.png", content) + require.Equal(t, codes.Unauthenticated, status.Code(err)) + + _, err = replaceContent(ownerCtx, svc, "attachments/missing", "drawing.canvas.png", content) + require.Equal(t, codes.NotFound, status.Code(err)) + + _, err = svc.UploadAttachment(ownerCtx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: "drawing.canvas.png", Type: "image/png"}, + AttachmentId: "chosen-id", + ReplaceAttachment: attachment.Name, + }}, + }) + require.Equal(t, codes.InvalidArgument, status.Code(err), "a replacement keeps the attachment's ID") + + linked, err := stores.CreateAttachment(ctx, &store.Attachment{ + UID: "remote-link", + CreatorID: owner.ID, + Filename: "remote.png", + Type: "image/png", + StorageType: storepb.AttachmentStorageType_EXTERNAL, + Reference: "https://example.com/remote.png", + }) + require.NoError(t, err) + _, err = replaceContent(ownerCtx, svc, "attachments/"+linked.UID, "drawing.canvas.png", content) + require.Equal(t, codes.FailedPrecondition, status.Code(err), "an external link has no stored content") + + // An admin may replace anyone's attachment; the owner keeps it. + replaced, err := replaceContent(adminCtx, svc, attachment.Name, "drawing-2.canvas.png", content) + require.NoError(t, err) + require.Equal(t, attachment.Name, replaced.Name) + uid, err := api.ExtractAttachmentUIDFromName(attachment.Name) + require.NoError(t, err) + stored, err := stores.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + require.Equal(t, owner.ID, stored.CreatorID) +} + +func TestReplaceAttachmentContentWithMotionPhotoStaysPlain(t *testing.T) { + ctx := context.Background() + svc, _, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + owner, err := stores.CreateUser(ctx, &store.User{Username: "owner", Role: store.RoleUser, Email: "owner@example.com"}) + require.NoError(t, err) + ownerCtx := context.WithValue(ctx, auth.UserIDContextKey, owner.ID) + attachment := createDrawingInline(ownerCtx, t, svc, testutil.BuildPNG(4, 4)) + + motion := testutil.BuildMotionPhotoJPEG() + response, err := svc.UploadAttachment(ownerCtx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: "photo.jpg", Type: "image/jpeg"}, + TotalSize: int64(len(motion)), + ReplaceAttachment: attachment.Name, + }}, + Data: motion, + FinishWrite: true, + }) + require.NoError(t, err) + require.Nil(t, response.Attachment.MotionMedia, "a replacement is never part of a motion photo") + + // So it can be replaced again. + _, err = replaceContent(ownerCtx, svc, attachment.Name, "drawing.canvas.png", testutil.BuildPNG(4, 4)) + require.NoError(t, err) +} + +func TestReplaceAttachmentContentConcurrently(t *testing.T) { + ctx := context.Background() + svc, _, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + configureLocalStorage(ctx, t, stores) + owner, err := stores.CreateUser(ctx, &store.User{Username: "owner", Role: store.RoleUser, Email: "owner@example.com"}) + require.NoError(t, err) + ownerCtx := context.WithValue(ctx, auth.UserIDContextKey, owner.ID) + attachment := createDrawingInline(ownerCtx, t, svc, testutil.BuildPNG(4, 4)) + + const replacements = 6 + errs := make(chan error, replacements) + for i := range replacements { + go func() { + _, err := replaceContent(ownerCtx, svc, attachment.Name, fmt.Sprintf("drawing-%d.canvas.png", i), testutil.BuildPNG(4+i, 4+i)) + errs <- err + }() + } + for range replacements { + require.NoError(t, <-errs) + } + + // Only the file the row points at is left: no replacement orphaned another's. + uid, err := api.ExtractAttachmentUIDFromName(attachment.Name) + require.NoError(t, err) + stored, err := stores.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + files, err := filepath.Glob(filepath.Join(svc.Profile.Data, "assets", "*")) + require.NoError(t, err) + require.Equal(t, []string{localFilePath(svc.Profile.Data, stored.Reference)}, files) +} diff --git a/server/fileserver/drawing_png_test.go b/server/fileserver/drawing_png_test.go new file mode 100644 index 000000000..7b9f27c8e --- /dev/null +++ b/server/fileserver/drawing_png_test.go @@ -0,0 +1,127 @@ +package fileserver + +import ( + "bytes" + "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/usememos/memos/internal/httphandler" + "github.com/usememos/memos/internal/testutil/fakes3" + apipb "github.com/usememos/memos/proto/gen/api" + storepb "github.com/usememos/memos/proto/gen/store" + "github.com/usememos/memos/server/api" + "github.com/usememos/memos/server/auth" + "github.com/usememos/memos/store" +) + +// TestDrawingPNGRoundTripsUnchanged guards the drawing format of @usememos/canvas: a +// drawing is a PNG whose editable scene sits in a zTXt chunk ("memos-canvas"). Uploads +// of image/png must be stored and served byte for byte, never re-encoded or stripped +// of metadata, or "Edit drawing" would find no scene. The fixture is +// tests/fixtures/png/v1.canvas.png from usememos/canvas. +func TestDrawingPNGRoundTripsUnchanged(t *testing.T) { + fixture, err := os.ReadFile("testdata/v1.canvas.png") + require.NoError(t, err) + require.True(t, bytes.Contains(fixture, []byte("zTXtmemos-canvas\x00")), "fixture carries the scene chunk") + + storages := map[string]func(context.Context, *testing.T, *store.Store){ + "database": configureDatabaseStorage, + "local": configureLocalStorage, + "s3": func(ctx context.Context, t *testing.T, stores *store.Store) { + configureFakeS3Storage(ctx, t, stores, fakes3.New(t, "file-server-attachments")) + }, + } + uploads := map[string]func(context.Context, *testing.T, *api.APIService, []byte) *apipb.Attachment{ + "inline": createDrawingInline, + "chunked": uploadDrawingInChunks, + } + + for storageName, configure := range storages { + for uploadName, upload := range uploads { + t.Run(storageName+"/"+uploadName, func(t *testing.T) { + ctx := context.Background() + svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + configure(ctx, t, stores) + + creator, err := stores.CreateUser(ctx, &store.User{Username: "artist", Role: store.RoleUser, Email: "artist@example.com"}) + require.NoError(t, err) + creatorCtx := context.WithValue(ctx, auth.UserIDContextKey, creator.ID) + + attachment := upload(creatorCtx, t, svc, fixture) + require.Equal(t, int64(len(fixture)), attachment.Size) + _, err = svc.CreateMemo(creatorCtx, &apipb.CreateMemoRequest{Memo: &apipb.Memo{ + Content: "a drawing", + Visibility: apipb.Visibility_PUBLIC, + Attachments: []*apipb.Attachment{{Name: attachment.Name}}, + }}) + require.NoError(t, err) + + mux := http.NewServeMux() + fs.RegisterRoutes(mux, http.HandlerFunc(httphandler.NotFound)) + recorder := httptest.NewRecorder() + mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/file/%s/%s", attachment.Name, attachment.Filename), nil)) + + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, "image/png", recorder.Header().Get("Content-Type")) + require.Equal(t, fixture, recorder.Body.Bytes(), "the original file is served byte for byte") + }) + } + } +} + +const drawingFilename = "drawing-20261106-101500.canvas.png" + +// configureDatabaseStorage makes the database the instance's default +// attachment storage. Without a setting the test harness stores locally. +func configureDatabaseStorage(ctx context.Context, t *testing.T, stores *store.Store) { + t.Helper() + _, err := stores.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{ + Key: storepb.InstanceSettingKey_STORAGE, + Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{ + Storages: []*storepb.Storage{{Id: "database", Name: "Database", Type: storepb.StorageType_STORAGE_TYPE_DATABASE}}, + DefaultStorageId: "database", + }}, + }) + require.NoError(t, err) +} + +func createDrawingInline(ctx context.Context, t *testing.T, svc *api.APIService, content []byte) *apipb.Attachment { + t.Helper() + attachment, err := svc.CreateAttachment(ctx, &apipb.CreateAttachmentRequest{Attachment: &apipb.Attachment{ + Filename: drawingFilename, + Type: "image/png", + Content: content, + }}) + require.NoError(t, err) + return attachment +} + +// uploadDrawingInChunks uses the chunked upload the web editor uses for drafts. +func uploadDrawingInChunks(ctx context.Context, t *testing.T, svc *api.APIService, content []byte) *apipb.Attachment { + t.Helper() + half := len(content) / 2 + started, err := svc.UploadAttachment(ctx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_Spec{Spec: &apipb.UploadAttachmentSpec{ + Attachment: &apipb.Attachment{Filename: drawingFilename, Type: "image/png"}, + TotalSize: int64(len(content)), + }}, + Data: content[:half], + }) + require.NoError(t, err) + finished, err := svc.UploadAttachment(ctx, &apipb.UploadAttachmentRequest{ + Upload: &apipb.UploadAttachmentRequest_UploadId{UploadId: started.UploadId}, + WriteOffset: int64(half), + Data: content[half:], + FinishWrite: true, + }) + require.NoError(t, err) + require.NotNil(t, finished.Attachment) + return finished.Attachment +} diff --git a/server/fileserver/testdata/v1.canvas.png b/server/fileserver/testdata/v1.canvas.png new file mode 100644 index 0000000000000000000000000000000000000000..7b47c4705382e402043f9a095242e51cd7047ff0 GIT binary patch literal 242 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1SHiab7}&qc~v11CAq1&`Ng`)iFsv-#S9D; zb4=tT{Oxo2J#C zE_}q)>{;~Ev}sLa1*cFp<6U{Sb>D*=PG=p^jMWNf)SXc^v;Pj?`8COJ{9c(hZ`j1)tmyH!Mh&-Ta47LM3#~Yw=w{)}4kTKW+hC p%J1po7*a7O`A0vCM8{(WhT9YLFXp*!*aVbf@O1TaS?83{1OUVnU335d literal 0 HcmV?d00001 diff --git a/store/attachment.go b/store/attachment.go index 16555717a..b2b947690 100644 --- a/store/attachment.go +++ b/store/attachment.go @@ -71,12 +71,26 @@ type UpdateAttachment struct { Filename *string MemoID *int32 Payload *storepb.AttachmentPayload + // Content replaces the stored file. The caller has already written the new + // object; the previous one is the caller's to delete once this commits. + Content *AttachmentContent // Policy is present for transport-facing updates. Drivers discover the // current attachment binding and authorize any linked memo in the same // transaction as the update. Policy *MemoWritePolicy } +// AttachmentContent is where an attachment's file is stored and what it is. +type AttachmentContent struct { + Type string + Size int64 + StorageType storepb.AttachmentStorageType + // Reference is the local path of a LOCAL file; empty otherwise. + Reference string + // Blob holds the file of a DATABASE attachment; nil otherwise. + Blob []byte +} + type DeleteAttachment struct { ID int32 MemoID *int32 @@ -324,7 +338,7 @@ func (s *Store) deleteAttachmentStorageImpl(ctx context.Context, attachment *Att } } - s.deleteAttachmentDerivedCaches(attachment) + s.DeleteAttachmentDerivedCaches(attachment) return nil } @@ -382,11 +396,11 @@ func AttachmentNeedsInstanceStorageSetting(attachment *Attachment) bool { return attachment.Payload.GetS3Object() != nil } -// deleteAttachmentDerivedCaches removes every derived asset the file server +// DeleteAttachmentDerivedCaches removes every derived asset the file server // may have written for the attachment: the current thumbnail, its failure // marker, the pre-v2 thumbnail name, and the extracted motion clip. The names // must match server/fileserver. -func (s *Store) deleteAttachmentDerivedCaches(attachment *Attachment) { +func (s *Store) DeleteAttachmentDerivedCaches(attachment *Attachment) { for _, cachePath := range []string{ filepath.Join(s.profile.Data, thumbnailCacheFolder, attachment.UID+".v2.jpeg"), filepath.Join(s.profile.Data, thumbnailCacheFolder, attachment.UID+".v2.jpeg.failed"), diff --git a/store/db/mysql/attachment.go b/store/db/mysql/attachment.go index 7e9013ca6..8f52f410f 100644 --- a/store/db/mysql/attachment.go +++ b/store/db/mysql/attachment.go @@ -46,10 +46,7 @@ func (d *DB) CreateAttachment(ctx context.Context, create *store.Attachment) (*s func insertMySQLAttachment(ctx context.Context, executor memoUpdateExecer, create *store.Attachment) (int32, error) { fields := []string{"`uid`", "`filename`", "`blob`", "`type`", "`size`", "`creator_id`", "`memo_id`", "`storage_type`", "`reference`", "`payload`"} placeholder := []string{"?", "?", "?", "?", "?", "?", "?", "?", "?", "?"} - storageType := "" - if create.StorageType != storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED { - storageType = create.StorageType.String() - } + storageType := attachmentStorageTypeString(create.StorageType) payloadString := "{}" if create.Payload != nil { bytes, err := protojson.Marshal(create.Payload) @@ -285,6 +282,10 @@ func applyMySQLAttachmentUpdate(ctx context.Context, executor memoUpdateExecer, } set, args = append(set, "`payload` = ?"), append(args, string(bytes)) } + if c := update.Content; c != nil { + set = append(set, "`type` = ?", "`size` = ?", "`storage_type` = ?", "`reference` = ?", "`blob` = ?") + args = append(args, c.Type, c.Size, attachmentStorageTypeString(c.StorageType), c.Reference, c.Blob) + } if len(set) == 0 { return nil @@ -354,3 +355,11 @@ func (d *DB) GetAttachmentStorageUsage(ctx context.Context, creatorID int32) (in } return size, nil } + +// attachmentStorageTypeString is the storage_type column value: the enum name, or empty for unspecified. +func attachmentStorageTypeString(storageType storepb.AttachmentStorageType) string { + if storageType == storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED { + return "" + } + return storageType.String() +} diff --git a/store/db/postgres/attachment.go b/store/db/postgres/attachment.go index a3520a408..b9d3e053f 100644 --- a/store/db/postgres/attachment.go +++ b/store/db/postgres/attachment.go @@ -45,10 +45,7 @@ type postgresAttachmentQueryRower interface { func insertPostgresAttachment(ctx context.Context, executor postgresAttachmentQueryRower, create *store.Attachment) (*store.Attachment, error) { fields := []string{"uid", "filename", "blob", "type", "size", "creator_id", "memo_id", "storage_type", "reference", "payload"} - storageType := "" - if create.StorageType != storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED { - storageType = create.StorageType.String() - } + storageType := attachmentStorageTypeString(create.StorageType) payloadString := "{}" if create.Payload != nil { bytes, err := protojson.Marshal(create.Payload) @@ -271,6 +268,13 @@ func applyPostgresAttachmentUpdate(ctx context.Context, executor memoUpdateExece } set, args = append(set, "payload = "+placeholder(len(args)+1)), append(args, string(bytes)) } + if c := update.Content; c != nil { + columns := []string{"type", "size", "storage_type", "reference", "blob"} + values := []any{c.Type, c.Size, attachmentStorageTypeString(c.StorageType), c.Reference, c.Blob} + for i, column := range columns { + set, args = append(set, column+" = "+placeholder(len(args)+1)), append(args, values[i]) + } + } if len(set) == 0 { return nil @@ -331,3 +335,11 @@ func (d *DB) GetAttachmentStorageUsage(ctx context.Context, creatorID int32) (in } return size, nil } + +// attachmentStorageTypeString is the storage_type column value: the enum name, or empty for unspecified. +func attachmentStorageTypeString(storageType storepb.AttachmentStorageType) string { + if storageType == storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED { + return "" + } + return storageType.String() +} diff --git a/store/db/sqlite/attachment.go b/store/db/sqlite/attachment.go index a7e8736c5..ba34e8c2e 100644 --- a/store/db/sqlite/attachment.go +++ b/store/db/sqlite/attachment.go @@ -42,10 +42,7 @@ func (d *DB) CreateAttachment(ctx context.Context, create *store.Attachment) (*s func insertSQLiteAttachment(ctx context.Context, executor dbExecutor, create *store.Attachment) (*store.Attachment, error) { fields := []string{"`uid`", "`filename`", "`blob`", "`type`", "`size`", "`creator_id`", "`memo_id`", "`storage_type`", "`reference`", "`payload`"} placeholder := []string{"?", "?", "?", "?", "?", "?", "?", "?", "?", "?"} - storageType := "" - if create.StorageType != storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED { - storageType = create.StorageType.String() - } + storageType := attachmentStorageTypeString(create.StorageType) payloadString := "{}" if create.Payload != nil { bytes, err := protojson.Marshal(create.Payload) @@ -264,6 +261,10 @@ func applySQLiteAttachmentUpdate(ctx context.Context, executor dbExecutor, updat } set, args = append(set, "`payload` = ?"), append(args, string(bytes)) } + if c := update.Content; c != nil { + set = append(set, "`type` = ?", "`size` = ?", "`storage_type` = ?", "`reference` = ?", "`blob` = ?") + args = append(args, c.Type, c.Size, attachmentStorageTypeString(c.StorageType), c.Reference, c.Blob) + } args = append(args, update.ID) stmt := "UPDATE `attachment` SET " + strings.Join(set, ", ") + " WHERE `id` = ?" @@ -316,3 +317,11 @@ func (d *DB) GetAttachmentStorageUsage(ctx context.Context, creatorID int32) (in } return size, nil } + +// attachmentStorageTypeString is the storage_type column value: the enum name, or empty for unspecified. +func attachmentStorageTypeString(storageType storepb.AttachmentStorageType) string { + if storageType == storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED { + return "" + } + return storageType.String() +} diff --git a/store/test/attachment_test.go b/store/test/attachment_test.go index b494808b7..2d3f813dc 100644 --- a/store/test/attachment_test.go +++ b/store/test/attachment_test.go @@ -525,6 +525,56 @@ func TestAttachmentUpdate(t *testing.T) { ts.Close() } +func TestAttachmentUpdateContent(t *testing.T) { + t.Parallel() + ctx := context.Background() + ts := NewTestingStore(ctx, t) + defer ts.Close() + + attachment, err := ts.CreateAttachment(ctx, &store.Attachment{ + UID: shortuuid.New(), + CreatorID: 101, + Filename: "drawing.canvas.png", + Blob: []byte("first drawing"), + Type: "application/octet-stream", + Size: 13, + }) + require.NoError(t, err) + + // Database content moves to a local file: the blob is cleared. + err = ts.UpdateAttachment(ctx, &store.UpdateAttachment{ + ID: attachment.ID, + Content: &store.AttachmentContent{ + Type: "image/png", + Size: 2048, + StorageType: storepb.AttachmentStorageType_LOCAL, + Reference: "assets/drawing.canvas.png", + }, + }) + require.NoError(t, err) + found, err := ts.GetAttachment(ctx, &store.FindAttachment{ID: &attachment.ID, GetBlob: true}) + require.NoError(t, err) + require.Equal(t, attachment.UID, found.UID) + require.Equal(t, "image/png", found.Type) + require.Equal(t, int64(2048), found.Size) + require.Equal(t, storepb.AttachmentStorageType_LOCAL, found.StorageType) + require.Equal(t, "assets/drawing.canvas.png", found.Reference) + require.Empty(t, found.Blob) + + // And back into the database: the reference is cleared. + err = ts.UpdateAttachment(ctx, &store.UpdateAttachment{ + ID: attachment.ID, + Content: &store.AttachmentContent{Type: "image/png", Size: 14, Blob: []byte("second drawing")}, + }) + require.NoError(t, err) + found, err = ts.GetAttachment(ctx, &store.FindAttachment{ID: &attachment.ID, GetBlob: true}) + require.NoError(t, err) + require.Equal(t, int64(14), found.Size) + require.Equal(t, storepb.AttachmentStorageType_ATTACHMENT_STORAGE_TYPE_UNSPECIFIED, found.StorageType) + require.Empty(t, found.Reference) + require.Equal(t, []byte("second drawing"), found.Blob) +} + func TestAttachmentGetByUID(t *testing.T) { t.Parallel() ctx := context.Background() diff --git a/web/package.json b/web/package.json index 738cfe612..4c9be0eab 100644 --- a/web/package.json +++ b/web/package.json @@ -26,8 +26,9 @@ "@tailwindcss/vite": "^4.2.4", "@tanstack/react-query": "^5.100.9", "@tanstack/react-query-devtools": "^5.100.9", + "@usememos/canvas": "26.11.2", "@usememos/editor": "26.11.0", - "@usememos/ui": "26.11.7", + "@usememos/ui": "26.11.9", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "copy-to-clipboard": "^4.0.2", diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index ce0713234..e081b47dc 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -38,12 +38,15 @@ importers: '@tanstack/react-query-devtools': specifier: ^5.100.9 version: 5.100.9(@tanstack/react-query@5.100.9(react@19.2.6))(react@19.2.6) + '@usememos/canvas': + specifier: 26.11.2 + version: 26.11.2(react-dom@19.2.6(react@19.2.6))(react@19.2.6) '@usememos/editor': specifier: 26.11.0 version: 26.11.0(react-dom@19.2.6(react@19.2.6))(react@19.2.6) '@usememos/ui': - specifier: 26.11.7 - version: 26.11.7(@base-ui/react@1.9.0(@types/react@19.2.14)(react-dom@19.2.6(react@19.2.6))(react@19.2.6))(lucide-react@1.34.0(react@19.2.6))(react-dom@19.2.6(react@19.2.6))(react@19.2.6) + specifier: 26.11.9 + version: 26.11.9(@base-ui/react@1.9.0(@types/react@19.2.14)(react-dom@19.2.6(react@19.2.6))(react@19.2.6))(lucide-react@1.34.0(react@19.2.6))(react-dom@19.2.6(react@19.2.6))(react@19.2.6) class-variance-authority: specifier: ^0.7.1 version: 0.7.1 @@ -948,6 +951,17 @@ packages: cpu: [x64] os: [win32] + '@usememos/canvas@26.11.2': + resolution: {integrity: sha512-sgFrRUSUz5qq3JMiVqPVsLd8NPL+nmWfuEG45mpjbvBbPRxHdKSzNVYc6DwokG/Z670zJI3gDc5Lfsi75QYvGw==} + peerDependencies: + react: '>=18' + react-dom: '>=18' + peerDependenciesMeta: + react: + optional: true + react-dom: + optional: true + '@usememos/editor@26.11.0': resolution: {integrity: sha512-8e0mcauyLH9ivn3oEKdwUO0cFeJdMjfMTfPgVvXf82D2IN4ekiBzaFjbVGjUbct4gDATVClywFPxQx9RreTbZw==} peerDependencies: @@ -959,8 +973,8 @@ packages: react-dom: optional: true - '@usememos/ui@26.11.7': - resolution: {integrity: sha512-DwDZMoBSNLuPb841L9E6P9++URgAeoekKIRgqdPzDqZt7/gZsp8TtuaF6pkuQabC4yvXU4VsnPmU4/O0iwrB7g==} + '@usememos/ui@26.11.9': + resolution: {integrity: sha512-rO0rmjrSwjMO60ftTV6yrmzrctF0Xtdqd8iT77vTYtVhd2ehl2KIqwym46nCLeQcp44oYFFt8rjtI6+Eqj57Aw==} peerDependencies: '@base-ui/react': 1.9.0 lucide-react: '>=1.0.0' @@ -2368,12 +2382,17 @@ snapshots: '@typescript/typescript-win32-x64@7.0.2': optional: true + '@usememos/canvas@26.11.2(react-dom@19.2.6(react@19.2.6))(react@19.2.6)': + optionalDependencies: + react: 19.2.6 + react-dom: 19.2.6(react@19.2.6) + '@usememos/editor@26.11.0(react-dom@19.2.6(react@19.2.6))(react@19.2.6)': optionalDependencies: react: 19.2.6 react-dom: 19.2.6(react@19.2.6) - '@usememos/ui@26.11.7(@base-ui/react@1.9.0(@types/react@19.2.14)(react-dom@19.2.6(react@19.2.6))(react@19.2.6))(lucide-react@1.34.0(react@19.2.6))(react-dom@19.2.6(react@19.2.6))(react@19.2.6)': + '@usememos/ui@26.11.9(@base-ui/react@1.9.0(@types/react@19.2.14)(react-dom@19.2.6(react@19.2.6))(react@19.2.6))(lucide-react@1.34.0(react@19.2.6))(react-dom@19.2.6(react@19.2.6))(react@19.2.6)': dependencies: '@base-ui/react': 1.9.0(@types/react@19.2.14)(react-dom@19.2.6(react@19.2.6))(react@19.2.6) lucide-react: 1.34.0(react@19.2.6) diff --git a/web/src/components/AppSidebar/TagsSection.tsx b/web/src/components/AppSidebar/TagsSection.tsx index 4dde17aba..f6a964d8f 100644 --- a/web/src/components/AppSidebar/TagsSection.tsx +++ b/web/src/components/AppSidebar/TagsSection.tsx @@ -108,7 +108,7 @@ const TagsSection = ({ tagCount, onSelect, scope }: Props) => { /> {t("common.more")} - + {t("common.tags")} }> diff --git a/web/src/components/AppSidebar/sidebar-layout.ts b/web/src/components/AppSidebar/sidebar-layout.ts index cd9e92feb..ab43db753 100644 --- a/web/src/components/AppSidebar/sidebar-layout.ts +++ b/web/src/components/AppSidebar/sidebar-layout.ts @@ -42,6 +42,7 @@ export const sidebarSurfaceVariants = cva("min-w-0 items-center", { * The scope panel that opens from the sidebar title is a list Popover with `mu-compact`, so its * rows are the package's menu rows at 28px. Each keeps a 20px leading slot, so icons, the avatar * and Space marks share one column; a Popover sets no highlight, so the rows add their own hover. + * A row whose own menu is open takes the package's highlight. */ -export const SCOPE_PANEL_ROW_CLASSES = `${menuClasses().item} transition-colors hover:bg-mu-hover data-popup-open:bg-mu-hover aria-[current=page]:bg-mu-hover mu-focus`; +export const SCOPE_PANEL_ROW_CLASSES = `${menuClasses().item} transition-colors hover:bg-mu-hover aria-[current=page]:bg-mu-hover mu-focus`; export const SCOPE_PANEL_ICON_CLASSES = "mu-icon text-mu-ink-muted"; diff --git a/web/src/components/DrawingSheet/DrawingFrame.tsx b/web/src/components/DrawingSheet/DrawingFrame.tsx new file mode 100644 index 000000000..760a65ff9 --- /dev/null +++ b/web/src/components/DrawingSheet/DrawingFrame.tsx @@ -0,0 +1,66 @@ +import { Dialog } from "@usememos/ui"; +import type { ReactNode } from "react"; +import useMediaQuery from "@/hooks/useMediaQuery"; +import { cn } from "@/lib/utils"; + +/** + * The dialog a drawing opens in, for viewing and editing alike. From `md` up it nearly fills + * the window, 24px from each edge, so the memo stays in view around it; narrower, it fills the + * screen. The drawing's paper fills it edge to edge, and the controls float on the paper. + * One Popup with changing props, so crossing the breakpoint never remounts the canvas. + */ +export const DrawingDialogPopup = ({ label, children }: { label: string; children: ReactNode }) => { + const isInset = useMediaQuery("md"); + return ( + + {children} + + ); +}; + +/** + * A row of floating bars along the paper's top or bottom edge, clear of a phone's safe areas. + * Only the bars take pointer events; the paper between them stays drawable. + */ +export const FloatingRow = ({ edge, className, children }: { edge: "top" | "bottom"; className?: string; children: ReactNode }) => ( +
+ {children} +
+); + +/** + * A lone action on the paper, without a bar: the button is its own surface. Light in every + * theme, like the paper under it. + */ +export const FloatingAction = ({ children }: { children: ReactNode }) => ( +
+ {children} +
+); + +/** + * A group of controls floating on the paper: one raised bar with flat controls inside, so no + * shadow sits inside another. Light in every theme, like the paper under it. + */ +export const FloatingBar = ({ className, children }: { className?: string; children: ReactNode }) => ( +
+ {children} +
+); diff --git a/web/src/components/DrawingSheet/DrawingSheet.tsx b/web/src/components/DrawingSheet/DrawingSheet.tsx new file mode 100644 index 000000000..58013b9f3 --- /dev/null +++ b/web/src/components/DrawingSheet/DrawingSheet.tsx @@ -0,0 +1,465 @@ +import { type CanvasError, type CanvasHandle, drawingFileName, type Scene, type ToolName } from "@usememos/canvas"; +import { Canvas } from "@usememos/canvas/react"; +import { AlertDialog, Button, Dialog, Popover, Toggle, ToggleGroup, Tooltip } from "@usememos/ui"; +import { + CircleIcon, + EraserIcon, + ImagePlusIcon, + MousePointer2Icon, + MoveUpRightIcon, + PencilIcon, + Redo2Icon, + ScanIcon, + SquareIcon, + TypeIcon, + Undo2Icon, +} from "lucide-react"; +import { type ClipboardEvent, type ReactElement, useEffect, useRef, useState } from "react"; +import useMediaQuery from "@/hooks/useMediaQuery"; +import { showErrorToast } from "@/lib/toast"; +import { cn } from "@/lib/utils"; +import { type Translations, useTranslate } from "@/utils/i18n"; +import { primaryModifierGlyph } from "@/utils/platform"; +import { DrawingDialogPopup, FloatingAction, FloatingBar, FloatingRow } from "./DrawingFrame"; +import { DRAWING_HUES, useCanvasTheme } from "./useCanvasTheme"; + +const TOOLS: { name: ToolName; label: Translations; shortcut: string; icon: ReactElement }[] = [ + { name: "select", label: "editor.drawing.tool-select", shortcut: "V", icon: }, + { name: "pen", label: "editor.drawing.tool-pen", shortcut: "P", icon: }, + { name: "eraser", label: "editor.drawing.tool-eraser", shortcut: "E", icon: }, + { name: "rectangle", label: "editor.drawing.tool-rectangle", shortcut: "R", icon: }, + { name: "ellipse", label: "editor.drawing.tool-ellipse", shortcut: "O", icon: }, + { name: "arrow", label: "editor.drawing.tool-arrow", shortcut: "A", icon: }, + { name: "text", label: "editor.drawing.tool-text", shortcut: "T", icon: }, +]; +// One stable list, so the canvas does not reset its tools on every render. +const TOOL_NAMES = TOOLS.map((tool) => tool.name); + +const WIDTHS: { value: number; label: Translations }[] = [ + { value: 1, label: "editor.drawing.width-thin" }, + { value: 2, label: "editor.drawing.width-medium" }, + { value: 4, label: "editor.drawing.width-thick" }, +]; + +export interface DrawingSheetProps { + /** `edit` reopens a saved drawing; it changes the sheet's name and the discard prompt. */ + mode: "new" | "edit"; + /** The scene read from the drawing's original file. */ + initialScene?: Scene; + /** A picture to draw on: a file named like a drawing whose scene could not be read. */ + background?: Blob; + /** + * The finished drawing, a PNG with the scene embedded, named `drawing-YYYYMMDD-HHMMSS.canvas.png`. + * A promise keeps Done busy until it settles; a rejection keeps the sheet open to retry. + */ + onDone: (file: File) => void | Promise; + onClose: () => void; +} + +/** + * The drawing sheet: the paper fills the drawing dialog, with the controls floating on it. + * `@usememos/canvas` draws and edits the scene; everything around it (the controls, strings, + * theme, discard prompt) is memos'. On a phone the tools float at the bottom, in thumb + * reach, with each control 44 px apart. + */ +const DrawingSheet = ({ mode, initialScene, background, onDone, onClose }: DrawingSheetProps) => { + const t = useTranslate(); + const isWide = useMediaQuery("sm"); + // Small controls on a desktop; a phone keeps the 32px ones, 44px apart, for touch. + const size = isWide ? "sm" : undefined; + const { theme, ink, fontFamily } = useCanvasTheme(); + const canvasRef = useRef(null); + const surfaceRef = useRef(null); + const fileInputRef = useRef(null); + const [tool, setTool] = useState("pen"); + const [history, setHistory] = useState({ undo: false, redo: false }); + // Null follows the theme's ink, so a drawing started in one theme keeps reading in the other. + const [color, setColor] = useState(null); + const [width, setWidth] = useState(2); + const [isExporting, setIsExporting] = useState(false); + const [discardOpen, setDiscardOpen] = useState(false); + // Drawing waits for a background picture, so no stroke can land before it and be marked saved with it. + const [isPreparing, setIsPreparing] = useState(Boolean(background)); + const title = t(mode === "edit" ? "editor.drawing.edit" : "editor.drawing.new-title"); + const currentColor = color ?? ink; + + // Runs once the canvas has mounted (see CanvasMounted): the pen is ready, and a + // picture without a scene becomes the drawing's locked background. + const setUpCanvas = () => { + const canvas = canvasRef.current; + if (!canvas) return; + canvas.setTool("pen"); + if (!background) return; + canvas + .insertImage(background, { background: true }) + .then(() => { + // The picture is where the drawing starts, not a change to discard. + canvas.markSaved(); + canvas.zoomToFit(); + }) + .catch(() => showErrorToast(t("editor.drawing.image-failed"))) + .finally(() => setIsPreparing(false)); + }; + + const syncHistory = () => { + const canvas = canvasRef.current; + setHistory({ undo: Boolean(canvas?.canUndo()), redo: Boolean(canvas?.canRedo()) }); + }; + + const handleCanvasError = (error: CanvasError) => { + if (error.operation === "insert-image" || error.operation === "load-image") showErrorToast(t("editor.drawing.image-failed")); + else if (error.operation === "export-png") showErrorToast(t("editor.drawing.export-failed")); + // A face of the UI font stack that cannot load (a local() alias, say) falls back to the + // next one in the stack; nothing for the person to act on. + }; + + const chooseTool = (next: ToolName) => { + setTool(next); + canvasRef.current?.setTool(next); + }; + + const applyWidth = (strokeWidth: number) => { + canvasRef.current?.setStyle({ strokeWidth }); + setWidth(strokeWidth); + }; + + const insertImages = (files: File[]) => { + for (const file of files.filter((candidate) => candidate.type.startsWith("image/"))) { + canvasRef.current?.insertImage(file).catch(() => showErrorToast(t("editor.drawing.image-failed"))); + } + }; + + // The drawing surface inserts pasted pictures itself; a paste anywhere else on the sheet lands there too. + const handlePaste = (event: ClipboardEvent) => { + if (surfaceRef.current?.contains(event.target as Node)) return; + insertImages(Array.from(event.clipboardData.files)); + }; + + // Cancel, Escape and the system back gesture all ask first when the drawing has changes. + const requestClose = () => { + if (canvasRef.current?.isDirty()) { + setDiscardOpen(true); + return; + } + onClose(); + }; + + const handleDone = async () => { + const canvas = canvasRef.current; + if (!canvas || isExporting || isPreparing) return; + // Text still being typed is part of the drawing. + canvas.commitText(); + setIsExporting(true); + try { + const blob = await canvas.exportPng(); + await onDone(new File([blob], drawingFileName(), { type: "image/png" })); + // Only once Done went through: after a failed save, Cancel still asks before discarding. + // The host may have closed the sheet already. + canvasRef.current?.markSaved(); + } catch (error) { + console.error(error); + showErrorToast(t("editor.drawing.export-failed")); + } finally { + setIsExporting(false); + } + }; + + const toolRow = ( +
+ {TOOLS.map((item) => ( + + chooseTool(item.name)} + /> + } + /> + {t(item.label)} + + ))} +
+ ); + + const swatches = [{ key: "ink", value: ink }, ...DRAWING_HUES.map((hue) => ({ key: hue.key, value: hue.value }))] as const; + const styleControl = ( + + + } + /> + } + /> + } + /> + {t("editor.drawing.style")} + + +
+ {t("editor.drawing.color")} +
+ {swatches.map((swatch) => ( + + { + canvasRef.current?.setStyle({ strokeColor: swatch.value }); + setColor(swatch.key === "ink" ? null : swatch.value); + }} + icon={} + /> + } + /> + {t(`editor.drawing.color-${swatch.key}`)} + + ))} +
+
+
+ {t("editor.drawing.width")} + next && applyWidth(Number(next))} + > + {WIDTHS.map((option) => ( + + {t(option.label)} + + ))} + +
+
+
+ ); + + const modifier = primaryModifierGlyph(); + const historyControls = ( + <> + } + disabled={!history.undo} + onClick={() => { + canvasRef.current?.undo(); + syncHistory(); + }} + /> + } + disabled={!history.redo} + onClick={() => { + canvasRef.current?.redo(); + syncHistory(); + }} + /> + + ); + const viewControls = ( + <> + } onClick={() => canvasRef.current?.zoomToFit()} /> + } + onClick={() => fileInputRef.current?.click()} + /> + + ); + const cancelButton = ( + + ); + const doneButton = ( + + ); + + return ( + { + if (open) return; + // The sheet closes only through requestClose, which asks first when there are changes. + eventDetails.cancel(); + // Escape on the drawing surface leaves the current tool or selection; it belongs to the canvas. + const escapeOnSurface = eventDetails.reason === "escape-key" && surfaceRef.current?.contains(eventDetails.event.target as Node); + if (!escapeOnSurface) requestClose(); + }} + > + +
+ {title} + + {/* Undo and redo follow every gesture and shortcut as it ends; onChange alone is debounced. */} +
+ + +
+ + {isWide ? ( + + {cancelButton} + + {toolRow} + + {styleControl} + + {historyControls} + + {viewControls} + + {doneButton} + + ) : ( + <> + + {cancelButton} + + {historyControls} + {viewControls} + + {doneButton} + + {/* The tools in thumb reach. Under 380px the divider goes and the bar nears the + edges, so a 360px screen keeps 44px between targets. */} + + + {toolRow} + + {styleControl} + + + + )} + + { + insertImages(Array.from(event.target.files ?? [])); + event.target.value = ""; + }} + /> +
+ + + + + {t(mode === "edit" ? "editor.drawing.discard-edit-title" : "editor.drawing.discard-title")} + + + {t(mode === "edit" ? "editor.drawing.discard-edit-description" : "editor.drawing.discard-description")} + + + {t("editor.drawing.keep-drawing")}} /> + + + + +
+
+ ); +}; + +/** + * Calls back once, after the canvas beside it has mounted. The canvas creates its engine in + * a layout effect inside the dialog's portal, which mounts after the sheet's own effects; + * a sibling's passive effect runs after every layout effect, so the handle is live by then. + */ +const CanvasMounted = ({ onMounted }: { onMounted: () => void }) => { + const callback = useRef(onMounted); + useEffect(() => { + callback.current(); + }, []); + return null; +}; + +/** An icon-only ghost Button named by its Tooltip, as every icon control in memos. */ +const IconAction = ({ + label, + icon, + size, + shortcut, + disabled, + onClick, +}: { + label: string; + icon: ReactElement; + size?: "sm"; + shortcut?: string; + disabled?: boolean; + onClick: () => void; +}) => ( + + } /> + {label} + +); + +const Divider = ({ className }: { className?: string }) => ( + +); + +export default DrawingSheet; diff --git a/web/src/components/DrawingSheet/DrawingViewer.tsx b/web/src/components/DrawingSheet/DrawingViewer.tsx new file mode 100644 index 000000000..181c5cfbd --- /dev/null +++ b/web/src/components/DrawingSheet/DrawingViewer.tsx @@ -0,0 +1,166 @@ +import type { CanvasHandle, Scene } from "@usememos/canvas"; +import { Canvas } from "@usememos/canvas/react"; +import { Button, Dialog, Spinner, Tooltip } from "@usememos/ui"; +import { PencilLineIcon, ScanIcon, XIcon } from "lucide-react"; +import { type ReactElement, useEffect, useEffectEvent, useRef, useState } from "react"; +import { uploadService } from "@/components/MemoEditor/edit/services/uploadService"; +import useCurrentUser from "@/hooks/useCurrentUser"; +import useMediaQuery from "@/hooks/useMediaQuery"; +import { useUpdateMemo } from "@/hooks/useMemoQueries"; +import { showErrorToast } from "@/lib/toast"; +import { cn } from "@/lib/utils"; +import type { Attachment } from "@/types/proto/api/attachment_service_pb"; +import type { Memo } from "@/types/proto/api/memo_service_pb"; +import { useTranslate } from "@/utils/i18n"; +import { canManageAttachment } from "@/utils/user"; +import { DrawingDialogPopup, FloatingBar, FloatingRow } from "./DrawingFrame"; +import DrawingSheet from "./DrawingSheet"; +import { fetchDrawingFile, readDrawing } from "./drawing"; +import { useCanvasTheme } from "./useCanvasTheme"; + +export interface DrawingViewerProps { + attachment: Attachment; + /** The memo the drawing belongs to; with `canEdit`, Edit saves the drawing back to it. */ + memo: Pick; + /** Whether the viewer may change the memo: its author or an admin. */ + canEdit: boolean; + onClose: () => void; + /** The file carries no readable scene: show it as the plain image it is. */ + onNoScene: () => void; +} + +/** + * A drawing opened from a memo: the canvas in its read-only mode (pan, zoom, copy), with + * Zoom to fit and, for whoever may change the memo, Edit. Edit turns the viewer + * into the drawing sheet; Done replaces the drawing's file under the same name, stamps the + * memo's update time and returns to viewing the new version. An edit of someone else's + * drawing is added to the memo beside it. + */ +const DrawingViewer = ({ attachment: initialAttachment, memo, canEdit, onClose, onNoScene }: DrawingViewerProps) => { + const t = useTranslate(); + const currentUser = useCurrentUser(); + const isWide = useMediaQuery("sm"); + // Small controls on a desktop; a phone keeps the 32px ones for touch. + const size = isWide ? "sm" : undefined; + const { theme, fontFamily } = useCanvasTheme(); + const { mutateAsync: updateMemo } = useUpdateMemo(); + const canvasRef = useRef(null); + const [attachment, setAttachment] = useState(initialAttachment); + const [scene, setScene] = useState(); + const [isEditing, setIsEditing] = useState(false); + // An edit saved as a copy whose memo update failed: the user's own, so a retry replaces it + // instead of uploading another. + const unlinkedCopy = useRef(undefined); + // The load runs once per attachment; it reports through the host's committed callbacks. + const showNoScene = useEffectEvent(onNoScene); + const failToOpen = useEffectEvent(() => { + showErrorToast(t("editor.drawing.open-failed")); + onClose(); + }); + + useEffect(() => { + const controller = new AbortController(); + fetchDrawingFile(initialAttachment, controller.signal) + .then(readDrawing) + .then((content) => { + if (controller.signal.aborted) return; + if (content.scene) setScene(content.scene); + else showNoScene(); + }) + .catch((error) => { + if (controller.signal.aborted) return; + console.error(error); + failToOpen(); + }); + return () => controller.abort(); + }, [initialAttachment]); + + // Done in the sheet: the edit replaces the drawing's file. A drawing someone else uploaded + // stays, since only they or an admin may replace or remove it, and the edit joins the memo + // beside it. + const saveEdit = async (file: File) => { + const replace = canManageAttachment(attachment, currentUser); + const replaces = replace ? attachment.name : unlinkedCopy.current?.name; + const saved = await uploadService.uploadFile({ file, previewUrl: "", replaces }); + if (replace) { + await updateMemo({ update: { name: memo.name }, updateMask: ["update_time"] }); + } else { + unlinkedCopy.current = saved; + // Whole attachments, not names: the optimistic update shows them until the memo reloads. + const attachments = memo.attachments.flatMap((item) => (item.name === attachment.name ? [item, saved] : [item])); + await updateMemo({ update: { name: memo.name, attachments }, updateMask: ["attachments"] }); + unlinkedCopy.current = undefined; + } + const content = await readDrawing(file); + setAttachment(saved); + if (content.scene) setScene(content.scene); + setIsEditing(false); + }; + + if (isEditing && scene) { + return setIsEditing(false)} />; + } + + const title = t("editor.insert-menu.drawing"); + return ( + { + if (!open) onClose(); + }} + > + +
+ {scene ? ( + // No autofocus: a viewer should not draw a focus ring around the page as it opens. + + ) : ( +
+ +
+ )} + + {/* The caption matches the bar's button text: 13px beside small controls, 14px on a phone. + Not a Dialog.Title, whose heading type is 16px; the popup's aria-label names the dialog. */} + + } onClick={onClose} /> + {title} + + + } + onClick={() => canvasRef.current?.zoomToFit()} + /> + {canEdit && ( + // Flat inside the bar: the bar is the one raised surface. + + )} + + +
+
+
+ ); +}; + +/** An icon-only ghost Button named by its Tooltip. */ +const IconAction = ({ label, icon, size, onClick }: { label: string; icon: ReactElement; size?: "sm"; onClick: () => void }) => ( + + } /> + {label} + +); + +export default DrawingViewer; diff --git a/web/src/components/DrawingSheet/drawing.ts b/web/src/components/DrawingSheet/drawing.ts new file mode 100644 index 000000000..569da7dd3 --- /dev/null +++ b/web/src/components/DrawingSheet/drawing.ts @@ -0,0 +1,28 @@ +import type { Scene } from "@usememos/canvas"; +import type { Attachment } from "@/types/proto/api/attachment_service_pb"; +import { fetchAttachmentFile } from "@/utils/attachment"; + +/** + * A drawing is a PNG attachment with its editable scene embedded. The name is a hint + * (`drawing-YYYYMMDD-HHMMSS.canvas.png`); the scene chunk inside the file is the truth. + */ +export const DRAWING_FILENAME_SUFFIX = ".canvas.png"; + +export const isDrawingFilename = (filename: string): boolean => filename.toLowerCase().endsWith(DRAWING_FILENAME_SUFFIX); + +/** What "Edit drawing" opens: the scene read from the file, or, without one, the file as a background picture. */ +export type DrawingContent = { scene: Scene; background?: undefined } | { scene?: undefined; background: Blob }; + +/** + * Downloads the original file of a saved drawing. Never the thumbnail: that is a + * re-encoded JPEG without the scene. + */ +export const fetchDrawingFile = async (attachment: Attachment, signal?: AbortSignal): Promise => + (await fetchAttachmentFile(attachment, signal)).blob(); + +/** Reads the scene out of a drawing file. The canvas package loads only when a drawing is opened. */ +export const readDrawing = async (file: Blob): Promise => { + const { readScenePng } = await import("@usememos/canvas"); + const { scene } = await readScenePng(file); + return scene ? { scene } : { background: file }; +}; diff --git a/web/src/components/DrawingSheet/index.ts b/web/src/components/DrawingSheet/index.ts new file mode 100644 index 000000000..d2196cbb6 --- /dev/null +++ b/web/src/components/DrawingSheet/index.ts @@ -0,0 +1,12 @@ +import { lazy } from "react"; + +export type { DrawingSheetProps } from "./DrawingSheet"; +export type { DrawingViewerProps } from "./DrawingViewer"; +export { type DrawingContent, fetchDrawingFile, isDrawingFilename, readDrawing } from "./drawing"; +export { DRAWING_PAPER } from "./useCanvasTheme"; + +/** The sheet and the canvas engine load on the first drawing, not with the editor. */ +export const DrawingSheet = lazy(() => import("./DrawingSheet")); + +/** The read-only viewer a drawing opens in from a memo; loads with the canvas engine on first use. */ +export const DrawingViewer = lazy(() => import("./DrawingViewer")); diff --git a/web/src/components/DrawingSheet/useCanvasTheme.ts b/web/src/components/DrawingSheet/useCanvasTheme.ts new file mode 100644 index 000000000..dbcaf4f1a --- /dev/null +++ b/web/src/components/DrawingSheet/useCanvasTheme.ts @@ -0,0 +1,43 @@ +import type { CanvasTheme } from "@usememos/canvas"; +import { useState } from "react"; + +/** + * Every drawing is a sheet of the same paper, whatever memos' theme: it reads the same on a + * light or a dark card, and anywhere its PNG goes. The paper is stored in the drawing, so a + * drawing made before keeps the paper it was made with. + */ +export const DRAWING_PAPER = "#fbfaf7"; + +/** Dark ink for the light paper: the default pen and the palette's first color. */ +export const DRAWING_INK = "#1a1a1a"; + +/** The light theme's accent, since selection and handles are drawn on the light paper. */ +const DRAWING_ACCENT = "oklch(0.5 0.13 255)"; + +/** Seven hues that read on the paper; the palette's first color is the ink. */ +export const DRAWING_HUES = [ + { key: "gray", value: "#868e96" }, + { key: "red", value: "#e03131" }, + { key: "orange", value: "#f08c00" }, + { key: "green", value: "#2f9e44" }, + { key: "teal", value: "#0c8599" }, + { key: "blue", value: "#1971c2" }, + { key: "violet", value: "#7048e8" }, +] as const; + +const DRAWING_THEME: CanvasTheme = { + paper: DRAWING_PAPER, + text: DRAWING_INK, + selection: DRAWING_ACCENT, + handle: DRAWING_ACCENT, + palette: [DRAWING_INK, ...DRAWING_HUES.map((hue) => hue.value)], +}; + +/** + * The canvas theme: the fixed paper and ink, with text in memos' sans-serif stack as the + * browser resolved it. The sheet's own bars still follow memos' theme around the paper. + */ +export const useCanvasTheme = () => { + const [fontFamily] = useState(() => getComputedStyle(document.documentElement).getPropertyValue("--mu-font-sans").trim()); + return { theme: DRAWING_THEME, ink: DRAWING_INK, fontFamily }; +}; diff --git a/web/src/components/MemoDetailSidebar/MemoDetailSidebar.tsx b/web/src/components/MemoDetailSidebar/MemoDetailSidebar.tsx index 0625e5bbd..efb7c6b15 100644 --- a/web/src/components/MemoDetailSidebar/MemoDetailSidebar.tsx +++ b/web/src/components/MemoDetailSidebar/MemoDetailSidebar.tsx @@ -230,7 +230,7 @@ const MemoDetailSidebar = ({ {t("common.share")} - + }> {t("memo.copy-link")} diff --git a/web/src/components/MemoEditor/README.md b/web/src/components/MemoEditor/README.md index c90946ff8..638b42cda 100644 --- a/web/src/components/MemoEditor/README.md +++ b/web/src/components/MemoEditor/README.md @@ -53,6 +53,10 @@ Formatting is the editor's own: its toolbar appears over selected text, the slas `useMemoSave` validates the draft, uploads local files, and hands the save to `memoService.save` with the operations it runs: the server's current copy (fetched into the detail cache to diff against), `useUpdateMemo` and `useCreateMemo`. Hosts that close after saving (edit, comment) hold a brief "Saved" confirmation before the session resets; the in-place composer resets at once. Uploads that landed before a failed save stay on the draft as attachments so a retry does not upload them again. +### Drawings + +A drawing is a PNG attachment made with [`@usememos/canvas`](https://github.com/usememos/canvas) (pinned to an exact version: it uses CalVer, so a version number does not signal breaking changes). The editable scene sits inside the file in a `zTXt` chunk named `memos-canvas`, and the name ends in `.canvas.png`. Everywhere outside the editor it is a plain image. The + menu's "Drawing" and the attachment row's "Edit drawing" open `components/DrawingSheet`, loaded on first use. It is a dialog that sits 24px inside the window from `md` up and fills the screen below that, with its controls floating on the paper (`DrawingFrame`). The package draws and edits the scene; the controls, strings, paper (`DRAWING_PAPER`, the same in every theme) and discard prompt are memos'. In a memo, a drawing opens in `DrawingViewer`, the same dialog with the canvas read-only, whose Edit saves in place. `useDrawing` owns the session: Done joins the draft through the same `LocalFile` path as a picked image, so the memo's save uploads it. "Edit drawing" reads the original file, never the thumbnail. Done on a file still in the draft swaps it in place. Done on a saved drawing keeps the attachment in the draft and queues the edit as a `LocalFile` with `replaces` set: the row shows the edit, and the save uploads it with `UploadAttachmentSpec.replace_attachment`, so the attachment keeps its name and place (`mergeUploadedAttachments`) while the server swaps the file and drops its thumbnail. Removing the pending row drops the edit. A saved drawing someone else uploaded stays, since only they or an admin may replace or remove it; its edit joins the draft as a new attachment. A file with the suffix but no readable scene opens as the background of a new drawing. The server must store `image/png` uploads unchanged, which `server/fileserver/drawing_png_test.go` checks. + ### Suggestions Hosts of a new top-level memo pass `useMemoSuggestions()` into `suggestions`; edits and comments show none. `EditorSuggestions` hides until the draft has text, drops suggestions already satisfied or accepted, and shows at most three. Acceptance history lives in the store so Undo does not immediately resuggest, and clears with the draft. diff --git a/web/src/components/MemoEditor/edit/EditSession.tsx b/web/src/components/MemoEditor/edit/EditSession.tsx index 79196b863..80f9f1933 100644 --- a/web/src/components/MemoEditor/edit/EditSession.tsx +++ b/web/src/components/MemoEditor/edit/EditSession.tsx @@ -1,4 +1,5 @@ -import { useCallback, useEffect, useMemo, useRef, useState } from "react"; +import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react"; +import { DrawingSheet } from "@/components/DrawingSheet"; import { useAuth } from "@/contexts/AuthContext"; import { useInstance } from "@/contexts/InstanceContext"; import useCurrentUser from "@/hooks/useCurrentUser"; @@ -20,6 +21,7 @@ import { useAudioRecorder, useAutoSave, useBlobUrls, + useDrawing, useFocusMode, useInlineImageUpload, useMemoInit, @@ -80,6 +82,11 @@ export const EditSession: React.FC = ({ const { createBlobUrl } = useBlobUrls(); const saveMediaMetadata = userGeneralSetting?.saveMediaMetadata ?? false; const inlineImageUpload = useInlineImageUpload(editorRef); + const toDrawingFile = useCallback( + (file: File) => toLocalFiles([file], { createBlobUrl, saveMediaMetadata })[0], + [createBlobUrl, saveMediaMetadata], + ); + const drawing = useDrawing(toDrawingFile); const memoName = memo?.name; const showTimestamp = Boolean(memoName || (!memo && hasTimestamp)); @@ -407,6 +414,8 @@ export const EditSession: React.FC = ({ onTranscribeAttachment={canTranscribe ? transcribeAttachment : undefined} transcribingAttachment={transcribingAttachment} transcriptionDisabled={isSaving || audioRecorder.isBusy || isTranscribingAudio} + onEditDrawing={drawing.openExisting} + openingDrawing={drawing.opening} /> {!memo && !parentMemoName && isInitialized && suggestions.length > 0 && ( @@ -422,9 +431,23 @@ export const EditSession: React.FC = ({ viewToggles={viewToggles} sourceMode={sourceMode} onInsertImages={handleInsertImages} + onDrawingClick={drawing.openNew} /> + + {drawing.session && ( + + + + )} ); }; diff --git a/web/src/components/MemoEditor/edit/Toolbar/EditorToolbar.tsx b/web/src/components/MemoEditor/edit/Toolbar/EditorToolbar.tsx index 4e4466da0..3742b6213 100644 --- a/web/src/components/MemoEditor/edit/Toolbar/EditorToolbar.tsx +++ b/web/src/components/MemoEditor/edit/Toolbar/EditorToolbar.tsx @@ -21,6 +21,7 @@ export const EditorToolbar: FC = ({ viewToggles, sourceMode, onInsertImages, + onDrawingClick, }) => { const t = useTranslate(); const { actions, dispatch } = useEditorContext(); @@ -117,6 +118,7 @@ export const EditorToolbar: FC = ({ viewToggles={viewToggles} sourceMode={sourceMode} onInsertImages={onInsertImages} + onDrawingClick={onDrawingClick} /> { const insertItems = [ { key: "attachment", label: t("editor.insert-menu.add-attachment"), icon: PaperclipIcon, onClick: handleAttachmentUploadClick }, { key: "inline-image", label: t("editor.insert-menu.insert-image"), icon: ImageIcon, onClick: handleInlineImageUploadClick }, + ...(props.onDrawingClick + ? [{ key: "drawing", label: t("editor.insert-menu.drawing"), icon: PencilLineIcon, onClick: props.onDrawingClick }] + : []), { key: "audio", label: t("editor.audio-recorder.trigger"), icon: MicIcon, onClick: props.onAudioRecorderClick }, { key: "link", label: t("editor.insert-menu.link-memo"), icon: LinkIcon, onClick: handleOpenLinkDialog }, { key: "location", label: t("editor.insert-menu.add-location"), icon: MapPinIcon, onClick: handleLocationClick }, diff --git a/web/src/components/MemoEditor/edit/components/EditorMetadata.tsx b/web/src/components/MemoEditor/edit/components/EditorMetadata.tsx index 9ed2729d7..6da29f21d 100644 --- a/web/src/components/MemoEditor/edit/components/EditorMetadata.tsx +++ b/web/src/components/MemoEditor/edit/components/EditorMetadata.tsx @@ -13,6 +13,8 @@ export const EditorMetadata: FC = ({ onTranscribeAttachment, transcribingAttachment, transcriptionDisabled, + onEditDrawing, + openingDrawing, }) => { const { actions, dispatch } = useEditorContext(); const attachments = useEditorSelector((s) => s.metadata.attachments); @@ -41,6 +43,8 @@ export const EditorMetadata: FC = ({ onTranscribeAttachment={onTranscribeAttachment} transcribingAttachment={transcribingAttachment} transcriptionDisabled={transcriptionDisabled} + onEditDrawing={onEditDrawing} + openingDrawing={openingDrawing} /> LocalFile) => { + const t = useTranslate(); + const currentUser = useCurrentUser(); + const { actions, dispatch, getState } = useEditorContext(); + const [session, setSession] = useState(); + // The row whose drawing is being downloaded and read. + const [opening, setOpening] = useState(); + const request = useRef(undefined); + const nextId = useRef(0); + + useEffect(() => () => request.current?.abort(), []); + + const openNew = useCallback(() => { + // A drawing still downloading must not replace the new sheet when it arrives. + request.current?.abort(); + request.current = undefined; + setOpening(undefined); + setSession({ id: ++nextId.current, mode: "new", target: { kind: "new" } }); + }, []); + + const openExisting = useCallback( + async (source: DrawingSource) => { + if (request.current) return; + const controller = new AbortController(); + request.current = controller; + const sourceId = source.kind === "local" ? source.localFile.previewUrl : source.attachment.name; + setOpening(sourceId); + try { + const file = source.kind === "local" ? source.localFile.file : await fetchDrawingFile(source.attachment, controller.signal); + const content = await readDrawing(file); + if (controller.signal.aborted) return; + const target: DrawingTarget = + source.kind === "local" + ? { kind: "local", previewUrl: sourceId } + : { kind: "attachment", name: sourceId, canReplace: canManageAttachment(source.attachment, currentUser) }; + setSession( + content.scene + ? { id: ++nextId.current, mode: "edit", target, scene: content.scene } + : // No scene inside: draw on the picture as a new drawing and leave the original alone. + { id: ++nextId.current, mode: "new", target: { kind: "new" }, background: content.background }, + ); + } catch (error) { + if (controller.signal.aborted) return; + console.error(error); + showErrorToast(t("editor.drawing.open-failed")); + } finally { + if (request.current === controller) request.current = undefined; + if (!controller.signal.aborted) setOpening(undefined); + } + }, + [currentUser, t], + ); + + const close = useCallback(() => setSession(undefined), []); + + const done = useCallback( + (file: File) => { + const target = session?.target ?? { kind: "new" }; + const localFile = toLocalFile(file); + const state = getState(); + if (target.kind === "local") { + // A file that is itself an edit of a saved drawing stays one. + dispatch( + actions.setLocalFiles( + state.localFiles.map((item) => (item.previewUrl === target.previewUrl ? { ...localFile, replaces: item.replaces } : item)), + ), + ); + } else if (target.kind === "attachment" && target.canReplace) { + // The saved drawing keeps its place; its edit uploads over it when the memo is saved. + dispatch( + actions.setLocalFiles([ + ...state.localFiles.filter((item) => item.replaces !== target.name), + { ...localFile, replaces: target.name }, + ]), + ); + } else { + // A new drawing, or an edit of one the user may not replace (someone else uploaded it, + // and only they or an admin may remove it): the edit joins the memo beside it. + dispatch(actions.addLocalFile(localFile)); + } + setSession(undefined); + }, + [actions, dispatch, getState, session, toLocalFile], + ); + + return { session, opening, openNew, openExisting, close, done }; +}; diff --git a/web/src/components/MemoEditor/edit/hooks/useMemoSave.ts b/web/src/components/MemoEditor/edit/hooks/useMemoSave.ts index 6153b10dc..366dba161 100644 --- a/web/src/components/MemoEditor/edit/hooks/useMemoSave.ts +++ b/web/src/components/MemoEditor/edit/hooks/useMemoSave.ts @@ -11,7 +11,7 @@ import type { Attachment } from "@/types/proto/api/attachment_service_pb"; import type { Visibility } from "@/types/proto/api/memo_service_pb"; import { useTranslate } from "@/utils/i18n"; import { errorService, memoService, validationService } from "../services"; -import type { MemoSaveOperations } from "../services/memoService"; +import { DrawingNotSavedError, type MemoSaveOperations, mergeUploadedAttachments } from "../services/memoService"; import { useEditorContext } from "../state"; import { restampDefaultCreateTime } from "../utils/deriveDefaultCreateTime"; @@ -77,7 +77,10 @@ export function useMemoSave({ if (moved.length === 0) return; dispatch( actions.setMetadata({ - attachments: [...current.metadata.attachments, ...moved.map((file) => uploaded.get(file.previewUrl) as Attachment)], + attachments: mergeUploadedAttachments( + current.metadata.attachments, + moved.map((file) => uploaded.get(file.previewUrl) as Attachment), + ), }), ); dispatch(actions.setLocalFiles(current.localFiles.filter((file) => !uploaded.has(file.previewUrl)))); @@ -160,6 +163,12 @@ export function useMemoSave({ void queryClient.invalidateQueries({ queryKey: attachmentKeys.lists() }); void queryClient.invalidateQueries({ queryKey: userKeys.stats() }); } + if (error instanceof DrawingNotSavedError) { + // The rest of the memo is saved; only the drawing waits for another save. + console.error(error); + showErrorToast(t("editor.drawing.saved-without-drawing")); + return; + } handleError(error, showErrorToast, { context: "Failed to save memo", fallbackMessage: errorService.getErrorMessage(error), diff --git a/web/src/components/MemoEditor/edit/services/memoService.ts b/web/src/components/MemoEditor/edit/services/memoService.ts index f72965a43..6cae26ce7 100644 --- a/web/src/components/MemoEditor/edit/services/memoService.ts +++ b/web/src/components/MemoEditor/edit/services/memoService.ts @@ -18,6 +18,17 @@ function toAttachmentReferences(attachments: Attachment[]): Attachment[] { return attachments.map((a) => create(AttachmentSchema, { name: a.name })); } +/** + * Adds uploaded attachments to the draft's. An upload that replaced an + * attachment's content comes back under the same name and takes its place. + */ +export function mergeUploadedAttachments(attachments: Attachment[], uploaded: Attachment[]): Attachment[] { + const byName = new Map(uploaded.map((attachment) => [attachment.name, attachment])); + const merged = attachments.map((attachment) => byName.get(attachment.name) ?? attachment); + const existing = new Set(attachments.map((attachment) => attachment.name)); + return [...merged, ...uploaded.filter((attachment) => !existing.has(attachment.name))]; +} + function buildUpdateMask( prevMemo: Memo, state: EditorState, @@ -84,6 +95,17 @@ function buildUpdateMask( return { mask, patch }; } +/** + * The memo's own changes saved, but uploading an edited drawing over its attachment + * failed afterwards. The edit stays in the draft, so saving again retries only it. + */ +export class DrawingNotSavedError extends Error { + constructor(cause: unknown) { + super("The memo was saved, but an edited drawing was not", { cause }); + this.name = "DrawingNotSavedError"; + } +} + /** The memo operations a save runs: the memo mutation hooks, so caches follow every save. */ export interface MemoSaveOperations { /** The memo as the server has it now, to diff the draft against. */ @@ -105,24 +127,45 @@ export const memoService = { }, operations: MemoSaveOperations, ): Promise<{ memoName: string; hasChanges: boolean; moved?: boolean }> { - // 1. Upload local files first - const newAttachments = await uploadService.uploadFiles(state.localFiles, options.onFileUploaded); - const allAttachments = [...state.metadata.attachments, ...newAttachments]; + // 1. Upload new files first. Edits of saved drawings (`replaces`) upload over + // their attachment, which is visible at once, so they wait until the memo + // itself is saved: a refused save then leaves every saved drawing as it was. + const additions = state.localFiles.filter((file) => !file.replaces); + const replacements = state.localFiles.filter((file) => file.replaces); + const newAttachments = await uploadService.uploadFiles(additions, options.onFileUploaded); + let allAttachments = mergeUploadedAttachments(state.metadata.attachments, newAttachments); // 2. Update existing memo if (options.memoName) { const prevMemo = await operations.getMemo(options.memoName); const { mask, patch } = buildUpdateMask(prevMemo, state, allAttachments); - if (mask.size === 0) { + if (mask.size === 0 && replacements.length === 0) { return { memoName: prevMemo.name, hasChanges: false }; } - const memo = await operations.updateMemo(patch, Array.from(mask)); + let memo = mask.size > 0 ? await operations.updateMemo(patch, Array.from(mask)) : prevMemo; + if (replacements.length > 0) { + try { + await uploadService.uploadFiles(replacements, options.onFileUploaded); + } catch (error) { + // With nothing else to save, the save simply failed; otherwise say what did land. + throw mask.size > 0 ? new DrawingNotSavedError(error) : error; + } + // A new drawing is an edit of the memo: stamp it, keeping a custom time the draft sets. + // The update returns the memo with its replaced files, which refreshes every cached copy; + // it sends no attachment list, so one added elsewhere meanwhile stays. + memo = await operations.updateMemo({ name: memo.name, updateTime: patch.updateTime }, ["update_time"]); + } return { memoName: memo.name, hasChanges: true, moved: mask.has("space") }; } - // 3. Create new memo or comment + // 3. Create new memo or comment. Its attachments are not visible to anyone + // until it exists, so replacements land before it is created; a failure then + // never leaves a created memo behind for a retry to duplicate. + if (replacements.length > 0) { + allAttachments = mergeUploadedAttachments(allAttachments, await uploadService.uploadFiles(replacements, options.onFileUploaded)); + } const memoData = create(MemoSchema, { content: state.content, visibility: state.metadata.visibility, diff --git a/web/src/components/MemoEditor/edit/services/transcriptionService.ts b/web/src/components/MemoEditor/edit/services/transcriptionService.ts index 3ca260b16..a3eca3935 100644 --- a/web/src/components/MemoEditor/edit/services/transcriptionService.ts +++ b/web/src/components/MemoEditor/edit/services/transcriptionService.ts @@ -3,7 +3,7 @@ import { Code, ConnectError } from "@connectrpc/connect"; import { aiServiceClient } from "@/connect"; import { TranscribeRequestSchema, TranscriptionAudioSchema } from "@/types/proto/api/ai_service_pb"; import type { Attachment } from "@/types/proto/api/attachment_service_pb"; -import { getAttachmentUrl } from "@/utils/attachment"; +import { fetchAttachmentFile } from "@/utils/attachment"; // Mirrors maxTranscriptionAudioSizeBytes in server/api/ai_service.go so an // oversized attachment is rejected before it is downloaded, with the server's error. @@ -35,8 +35,7 @@ const transcribe = async (content: Uint8Array, filename: string, contentType: st export const transcriptionService = { async transcribeAttachment(attachment: Attachment, signal?: AbortSignal): Promise { checkAudioSize(Number(attachment.size)); - const response = await fetch(getAttachmentUrl(attachment), { signal, credentials: "same-origin" }); - if (!response.ok) throw new Error(`Failed to download audio (${response.status})`); + const response = await fetchAttachmentFile(attachment, signal); checkAudioSize(Number(response.headers.get("Content-Length"))); const content = new Uint8Array(await response.arrayBuffer()); checkAudioSize(content.byteLength); diff --git a/web/src/components/MemoEditor/edit/services/uploadService.ts b/web/src/components/MemoEditor/edit/services/uploadService.ts index 34f82a150..240b1a153 100644 --- a/web/src/components/MemoEditor/edit/services/uploadService.ts +++ b/web/src/components/MemoEditor/edit/services/uploadService.ts @@ -21,6 +21,7 @@ export const uploadService = { mediaMetadata, }), totalSize: BigInt(file.size), + replaceAttachment: localFile.replaces ?? "", }); // The spec call carries no data so that retrying it after a lost response // can at worst orphan an upload, never create a duplicate attachment. diff --git a/web/src/components/MemoEditor/edit/types/attachment.ts b/web/src/components/MemoEditor/edit/types/attachment.ts index bbc082563..a8c30bc26 100644 --- a/web/src/components/MemoEditor/edit/types/attachment.ts +++ b/web/src/components/MemoEditor/edit/types/attachment.ts @@ -33,6 +33,11 @@ export interface LocalFile { * from a source with nothing to extract). The promise never rejects. */ readonly mediaMetadata?: Promise; + /** + * The saved attachment this file replaces the content of (an edited drawing). + * The attachment keeps its name and place; the file uploads over it on save. + */ + readonly replaces?: string; } const AUDIO_RECORDING_FILENAME_RE = /^(?:voice-(?:recording|note)|audio-recording)-(\d{8})-(\d{4,6})/i; @@ -203,3 +208,6 @@ export function separateMediaAndDocs(items: AttachmentItem[]): { media: Attachme return { media, docs }; } + +/** A drawing "Edit drawing" opens: a file still in the draft, or a saved attachment. */ +export type DrawingSource = { kind: "local"; localFile: LocalFile } | { kind: "attachment"; attachment: Attachment }; diff --git a/web/src/components/MemoEditor/edit/types/components.ts b/web/src/components/MemoEditor/edit/types/components.ts index fc0d1a0b2..461718c47 100644 --- a/web/src/components/MemoEditor/edit/types/components.ts +++ b/web/src/components/MemoEditor/edit/types/components.ts @@ -4,7 +4,7 @@ import type { Attachment } from "@/types/proto/api/attachment_service_pb"; import type { Location, Memo, Visibility } from "@/types/proto/api/memo_service_pb"; import type { EditorFileOrigin } from "../extensions"; import type { AudioRecorderStatus } from "../hooks/useAudioRecorder"; -import type { LocalFile } from "./attachment"; +import type { DrawingSource, LocalFile } from "./attachment"; /** What MemoEditor hands an editing session: its subject, the host's defaults, and where the draft lives. */ export interface EditSessionProps { @@ -88,6 +88,8 @@ export interface EditorToolbarProps { viewToggles?: EditorViewToggles; sourceMode: EditorSourceModeToggle; onInsertImages: (files: File[]) => void; + /** Opens the drawing sheet for a new drawing. */ + onDrawingClick?: () => void; } export interface EditorMetadataProps { @@ -98,6 +100,10 @@ export interface EditorMetadataProps { uploadingLocalFileURLs: ReadonlySet; onInsertAttachments: (attachments: Attachment[]) => void; onInsertLocalFiles: (localFiles: LocalFile[]) => void; + /** Opens a drawing attachment in the drawing sheet. */ + onEditDrawing?: (source: DrawingSource) => void; + /** The attachment row whose drawing is being opened. */ + openingDrawing?: string; } export interface AudioRecorderPanelProps { @@ -132,6 +138,7 @@ export interface InsertMenuProps { viewToggles?: EditorViewToggles; sourceMode: EditorSourceModeToggle; onInsertImages: (files: File[]) => void; + onDrawingClick?: () => void; } export interface AudienceMenuProps { diff --git a/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx b/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx index 9442c3f32..48206a855 100644 --- a/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx +++ b/web/src/components/MemoMetadata/Attachment/AttachmentListEditor.tsx @@ -7,11 +7,13 @@ import { FileTextIcon, ImagePlusIcon, PauseIcon, + PencilLineIcon, PlayIcon, XIcon, } from "lucide-react"; import { type FC, type MouseEvent, useMemo, useRef, useState } from "react"; -import type { AttachmentItem, LocalFile } from "@/components/MemoEditor/edit/types/attachment"; +import { isDrawingFilename } from "@/components/DrawingSheet"; +import type { AttachmentItem, DrawingSource, LocalFile } from "@/components/MemoEditor/edit/types/attachment"; import { getAudioRecordingTimeLabel, toAttachmentItems } from "@/components/MemoEditor/edit/types/attachment"; import MetadataSection, { METADATA_ROW_BOX_CLASSES, @@ -22,6 +24,7 @@ import MetadataSection, { MetadataRowDetail, } from "@/components/MemoMetadata/MetadataSection"; import PreviewImageDialog from "@/components/PreviewImageDialog"; +import useMediaQuery from "@/hooks/useMediaQuery"; import { cn } from "@/lib/utils"; import type { Attachment } from "@/types/proto/api/attachment_service_pb"; import { formatFileSize, getFileTypeLabel } from "@/utils/format"; @@ -47,22 +50,53 @@ interface AttachmentListEditorProps { onInsertLocalFiles?: (localFiles: LocalFile[]) => void; placementActionsDisabled?: boolean; uploadingLocalFileURLs?: ReadonlySet; + /** Opens a drawing (a `.canvas.png` image) in the drawing sheet. */ + onEditDrawing?: (source: DrawingSource) => void; + /** The row whose drawing is being opened. */ + openingDrawing?: string; +} + +interface RowAction { + label: string; + onClick: () => void; + disabled?: boolean; + busy?: boolean; } /** Trailing controls of an attachment row: quiet 24px squares that show once the row is engaged. */ const AttachmentItemActions: FC<{ transcriptionAction?: { label: string; onClick: () => void; disabled: boolean; busy: boolean }; + drawingAction?: RowAction; placementAction?: { label: string; onClick: () => void; disabled?: boolean }; onRemove?: () => void; onMoveUp?: () => void; onMoveDown?: () => void; canMoveUp?: boolean; canMoveDown?: boolean; -}> = ({ transcriptionAction, placementAction, onRemove, onMoveUp, onMoveDown, canMoveUp = true, canMoveDown = true }) => { +}> = ({ transcriptionAction, drawingAction, placementAction, onRemove, onMoveUp, onMoveDown, canMoveUp = true, canMoveDown = true }) => { const t = useTranslate(); + // A drawing row already carries Insert image; on narrow rows its edit action keeps only the glyph. + const isWide = useMediaQuery("md"); const stopPropagation = (event: MouseEvent) => { event.stopPropagation(); }; + const drawingButton = drawingAction && ( + + ); return (
@@ -82,6 +116,15 @@ const AttachmentItemActions: FC<{ {transcriptionAction.label} )} + {drawingButton && + (isWide ? ( + drawingButton + ) : ( + + + {drawingAction?.label} + + ))} {placementAction && (