@@ -6,6 +6,7 @@ import { z } from 'zod'
66import { requireUserId } from '#app/utils/auth.server.ts'
77import { prisma } from '#app/utils/db.server.ts'
88import { uploadHandler } from '#app/utils/file-uploads.server.ts'
9+ import { uploadNoteImage } from '#app/utils/storage.server.ts'
910import {
1011 MAX_UPLOAD_SIZE ,
1112 NoteEditorSchema ,
@@ -48,16 +49,18 @@ export async function action({ request }: ActionFunctionArgs) {
4849 } )
4950 }
5051 } ) . transform ( async ( { images = [ ] , ...data } ) => {
52+ const noteId = data . id ?? cuid ( )
5153 return {
5254 ...data ,
55+ id : noteId ,
5356 imageUpdates : await Promise . all (
5457 images . filter ( imageHasId ) . map ( async ( i ) => {
5558 if ( imageHasFile ( i ) ) {
5659 return {
5760 id : i . id ,
5861 altText : i . altText ,
5962 contentType : i . file . type ,
60- blob : Buffer . from ( await i . file . arrayBuffer ( ) ) ,
63+ storageKey : await uploadNoteImage ( userId , noteId , i . file ) ,
6164 }
6265 } else {
6366 return {
@@ -75,7 +78,7 @@ export async function action({ request }: ActionFunctionArgs) {
7578 return {
7679 altText : image . altText ,
7780 contentType : image . file . type ,
78- blob : Buffer . from ( await image . file . arrayBuffer ( ) ) ,
81+ storageKey : await uploadNoteImage ( userId , noteId , image . file ) ,
7982 }
8083 } ) ,
8184 ) ,
@@ -101,8 +104,9 @@ export async function action({ request }: ActionFunctionArgs) {
101104
102105 const updatedNote = await prisma . note . upsert ( {
103106 select : { id : true , owner : { select : { username : true } } } ,
104- where : { id : noteId ?? '__new_note__' } ,
107+ where : { id : noteId } ,
105108 create : {
109+ id : noteId ,
106110 ownerId : userId ,
107111 title,
108112 content,
@@ -115,7 +119,11 @@ export async function action({ request }: ActionFunctionArgs) {
115119 deleteMany : { id : { notIn : imageUpdates . map ( ( i ) => i . id ) } } ,
116120 updateMany : imageUpdates . map ( ( updates ) => ( {
117121 where : { id : updates . id } ,
118- data : { ...updates , id : updates . blob ? cuid ( ) : updates . id } ,
122+ data : {
123+ ...updates ,
124+ // If the image is new, we need to generate a new ID to bust the cache.
125+ id : updates . storageKey ? cuid ( ) : updates . id ,
126+ } ,
119127 } ) ) ,
120128 create : newImages ,
121129 } ,
0 commit comments