Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8
Feat/w3ds file uri#965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Feat/w3ds file uri #965
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4329ec8
add DigitalOcean Spaces object storage service to evault-core
coodos 54de4e8
add uploadFile GraphQL mutation backed by object storage
coodos 98371fc
add GET /files dereference endpoint redirecting to public URL
coodos e9d4ab2
add w3ds file URI parse and build utilities to web3-adapter
coodos 06dfe69
add w3ds file resolver and EVaultClient uploadFile support
coodos 1ac9ae8
wire __file mapping directive and document w3ds URI scheme
coodos ad14765
add array support to __file mapping directive
coodos e35fd72
apply __file directive to file fields across all platform mappings
coodos 2007c68
document array support for __file mapping directive
coodos b286aa3
fix biome formatting in web3-adapter file mapping code
coodos 192c8ef
harden uploadFile: validate base64, redirect scheme and clean up orph…
coodos 597dfc8
fix biome lint and clarify w3ds documentation examples
coodos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158 infrastructure/evault-core/src/core/protocol/graphql-server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,6 +10,8 @@ import { | ||
| computeEnvelopeHashForDelete, | ||
| } from "../db/envelope-hash"; | ||
| import { exampleQueries } from "./examples/examples"; | ||
| import { StorageService } from "../../services/StorageService"; | ||
| import { buildFileUri, FILE_SCHEMA_ID } from "../utils/w3ds-uri"; | ||
| import { typeDefs } from "./typedefs"; | ||
| import { VaultAccessGuard, type VaultContext } from "./vault-access-guard"; | ||
| import { MessageNotificationService } from "../../services/MessageNotificationService"; | ||
| @@ -1145,6 +1147,162 @@ export class GraphQLServer { | ||
| }; | ||
| }, | ||
| ), | ||
| // Upload a file to object storage and create a File meta-envelope | ||
| uploadFile: this.accessGuard.middleware( | ||
| async ( | ||
| _: any, | ||
| { | ||
| input, | ||
| }: { | ||
| input: { | ||
| filename: string; | ||
| contentType: string; | ||
| content: string; | ||
| acl: string[]; | ||
| }; | ||
| }, | ||
| context: VaultContext, | ||
| ) => { | ||
| if (!context.eName) { | ||
| return { | ||
| errors: [ | ||
| { | ||
| message: "X-ENAME header is required", | ||
| code: "MISSING_ENAME", | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| if (!StorageService.isConfigured()) { | ||
| return { | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Object storage is not configured on this eVault", | ||
| code: "STORAGE_NOT_CONFIGURED", | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| // Accept either raw base64 or a data: URI | ||
| const base64 = input.content.includes(",") | ||
| ? input.content.slice( | ||
| input.content.indexOf(",") + 1, | ||
| ) | ||
| : input.content; | ||
| // Strictly validate base64 before decoding — Buffer.from | ||
| // silently drops invalid characters, so malformed input | ||
| // must be rejected up-front. Padding ('=') is allowed | ||
| // only as the last 1-2 characters. | ||
| const isValidBase64 = | ||
| base64.length > 0 && | ||
| base64.length % 4 === 0 && | ||
| /^[A-Za-z0-9+/]+={0,2}$/.test(base64); | ||
| if (!isValidBase64) { | ||
| return { | ||
| errors: [ | ||
| { | ||
| field: "content", | ||
| message: "File content is empty or not valid base64", | ||
| code: "INVALID_CONTENT", | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const buffer = Buffer.from(base64, "base64"); | ||
| const MAX_FILE_BYTES = 50 * 1024 * 1024; // 50 MB | ||
| if (buffer.length > MAX_FILE_BYTES) { | ||
| return { | ||
| errors: [ | ||
| { | ||
| field: "content", | ||
| message: "File exceeds the 50 MB upload limit", | ||
| code: "FILE_TOO_LARGE", | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| // Track the uploaded object so a failed DB write can be | ||
| // compensated by deleting the now-orphaned blob. | ||
| let uploadedKey: string | null = null; | ||
| let storage: StorageService | null = null; | ||
| try { | ||
| const objectId = require("uuid").v4(); | ||
| const key = StorageService.buildKey( | ||
| context.eName, | ||
| input.filename, | ||
| objectId, | ||
| ); | ||
| storage = new StorageService(); | ||
| const publicUrl = await storage.uploadObject({ | ||
| buffer, | ||
| contentType: input.contentType, | ||
| key, | ||
| }); | ||
| uploadedKey = key; | ||
| const payload = { | ||
| filename: input.filename, | ||
| contentType: input.contentType, | ||
| size: buffer.length, | ||
| blobKey: key, | ||
| publicUrl, | ||
| uploadedAt: new Date().toISOString(), | ||
| }; | ||
| const result = await this.db.storeMetaEnvelope( | ||
| { | ||
| ontology: FILE_SCHEMA_ID, | ||
| payload, | ||
| acl: input.acl, | ||
| }, | ||
| input.acl, | ||
| context.eName, | ||
| ); | ||
| return { | ||
| uri: buildFileUri( | ||
| context.eName, | ||
| result.metaEnvelope.id, | ||
| ), | ||
| metaEnvelopeId: result.metaEnvelope.id, | ||
| publicUrl, | ||
| }; | ||
| } catch (error) { | ||
| console.error("uploadFile failed:", error); | ||
| // Compensating cleanup: if the blob was uploaded but | ||
| // a later step (DB write) failed, delete the now | ||
| // orphaned object so storage does not leak. | ||
| if (uploadedKey && storage) { | ||
| try { | ||
| await storage.deleteObject(uploadedKey); | ||
| } catch (cleanupError) { | ||
| console.error( | ||
| "uploadFile cleanup (delete orphaned object) failed:", | ||
| cleanupError, | ||
| ); | ||
| } | ||
| } | ||
| return { | ||
| errors: [ | ||
| { | ||
| message: | ||
| error instanceof Error | ||
| ? error.message | ||
| : "Failed to upload file", | ||
| code: "UPLOAD_FAILED", | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| }, | ||
| ), | ||
| updateMetaEnvelopeById: this.accessGuard.middleware( | ||
| async ( | ||
| _: any, | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.