Production File Storage and Attachment Architecture
Project: FamilyShell LLP — Health Records module
Area: Production debugging, Cloudflare R2, MongoDB, file storage, security, data migration
The Challenge
During my internship at FamilyShell LLP, I was working on the Health Records module of a production family management application.
While testing the attachment functionality, I discovered that documents older than a certain period were no longer opening.
Rather than treating this as an isolated frontend or API issue, I traced the complete attachment pipeline to determine where the failure originated.
Investigation
I found that MongoDB was storing presigned URLs for files stored in Cloudflare R2. Because presigned URLs are temporary, storing the URL itself meant that the database was effectively storing an expiring reference to the file.
This was particularly important because the application handles sensitive health-record documents. Simply switching to permanently public URLs would have introduced an unnecessary security concern.
I researched the available Cloudflare R2 access approaches and compared their implications for the application's requirements.
Architectural Decision
I proposed storing the file's object path rather than storing a temporary presigned URL.
When a user requests to view an attachment, the backend can use the stored object path to generate a new short-lived presigned URL.
This provides a better separation of concerns:
- MongoDB stores the persistent identity of the object.
- Cloudflare R2 stores the actual file.
- The backend controls access to the file.
- The generated URL can have a limited lifetime.
I discussed the findings and proposed architecture with the team and received approval to proceed with the change.
Handling Existing Production Data
The architecture could not simply be changed for new uploads because existing production records already contained presigned URLs.
For the old records, I extracted the object path from the existing presigned URL and used that path to generate a new short-lived URL when the user requested to view the attachment.
This allowed the application to move toward the new architecture without breaking access to existing files.
DOC and DOCX Handling
During the same work, I identified another issue. The application supported DOC and DOCX attachments, but the browser could not directly render those formats in the existing viewing flow.
Based on the application's requirements, those file types were changed to a download flow instead of browser rendering.
Preserving Original Filenames
This exposed another issue: downloaded files were using Cloudflare object identifiers as their filenames, which meant the user's original filename was lost.
I changed the MongoDB schema to preserve the original user-provided filename separately from the storage object identifier.
New files could therefore retain the user's filename when viewed or downloaded, while existing records could continue to work using their existing storage identifiers and filenames.
Engineering Principle
This challenge reinforced the importance of tracing a production problem through the complete data flow instead of fixing only the visible symptom.
It also taught me to consider security, existing production data, backward compatibility, and user experience together when changing backend architecture.