Text Editing Pipeline Review
This document summarizes how text files are edited in the application and lists potential issues observed in the current implementation.
Pipeline Overview
-
Viewing a file
KnowledgeViewer
builds a list ofworkBenchFiles
fromuseSessions()
and renders them throughFileContent
.FileContent
checks if the file URL is expired and, if necessary, fetches a fresh signed URL usinggetFabFileByIdFromServer
.- File contents are downloaded via
getContentFromFabfile
which internally performs anaxios.get
on the signed URL.
-
Editing a file
- When a file is opened for editing
KnowledgeModal
fetches the file viagetFabFileByIdFromServer
and loads its text usinggetContentFromFabfile
. - Edits are stored in the
editedContent
state and saved through thehandleSave
method. - Saving either calls
createFabFileOnServerWithUpload
for new files orupdateFileUtility
for existing ones. updateFileUtility
sends aPUT
request to/api/files/[id]
with the updated metadata andfileContent
.
- When a file is opened for editing
-
Server‐side update
- The API route (
pages/api/files/[id]/index.ts
) forwards the request tofabFilesService.updateFabFile
using the provided body. - In the manager layer (
server/managers/fabFileManager.ts
),updateFabFile
persists metadata to MongoDB and, when afilePath
is supplied, uploads the newfileContent
to S3:
- The API route (
if (updatedData.filePath && updatedFabFile.filePath === updatedData.filePath) {
const fileKey = updatedFabFile.filePath;
await filesStorage.upload(newContent ?? '', fileKey, {
ContentType: updatedFabFile.mimeType,
});
}
Potential Issues
- Missing body size limit on update API – Unlike
/api/files
which allows a 10 MB payload, the update route relies on Next.js defaults. Editing large files (>4 MB) will fail silently. - File content might not reach S3 –
fabFilesService.updateFabFile
is invoked with the request body, but the route does not separately pass thefileContent
argument required byupdateFabFile
in the manager. If the service does not forwardfileContent
, the upload step will be skipped and the old file remains in S3. - No signed URL refresh after save – After saving,
workBenchFiles
still contain the previousfileUrl
andfileUrlExpireAt
. If the URL is cached by the browser, users may continue to read stale content until it expires or they manually refresh. - Local state desync –
KnowledgeModal
updates Dexie but does not updateworkBenchFiles
inuseSessions
. Views relying on that state may still show the old file metadata. - MIME type handling – When renaming a file without an extension, the MIME type defaults to
text/plain
. Changing the extension does not update the MIME type, which may cause downstream processing errors.
Suggested Fixes
- Increase body size for updates
- Add
bodyParser: { sizeLimit: '10mb' }
to the config ofpages/api/files/[id]/index.ts
.
- Add
- Ensure file content is uploaded
- Verify that
fabFilesService.updateFabFile
forwards thefileContent
argument toupdateFabFile
. If not, adjust the service or API route to pass it explicitly.
- Verify that
- Refresh file metadata after save
- After a successful save, re-fetch the updated file or at least update
fileUrl
andfileUrlExpireAt
inworkBenchFiles
so subsequent reads use a fresh signed URL.
- After a successful save, re-fetch the updated file or at least update
- Synchronize local state
- Update the
workBenchFiles
array inuseSessions
whenhandleSave
completes to keep the viewer in sync.
- Update the
- Improve MIME type inference
- When the file name changes, infer the MIME type from its extension so uploads and downloads use the correct content type.
Implementing these adjustments should improve reliability so that text edits persist consistently.