-
Notifications
You must be signed in to change notification settings - Fork 121
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
Introduce file upload api #5977
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8dbf8d2
Checkout file upload endpoint related code
flvndvd a64f26f
Limit file upload to 1.
flvndvd b3c041c
Tmp
flvndvd e261d09
Implement FileResource
flvndvd 702bae3
:sparkles:
flvndvd c5c634f
Fix google-cloud/storage issue
flvndvd 95f9003
Add unique sId index
flvndvd 02065d7
Fix migration
flvndvd 5600005
:shirt:
flvndvd 3e88bc3
Make file upload rate limit more aggressive
flvndvd db82768
Enforce upload window
flvndvd 6980606
Ensure the uploaded file matches the requirements
flvndvd 2d0fead
:scissors:
flvndvd acae12e
Use fromidable to adhere to file requirements
flvndvd 1df3779
s/fetchByInternalId/fetchByModelId
flvndvd b3ac7ec
Move try/catch + move markAsFailed for preprocessing
flvndvd 0751f7c
s/fileRes/file
flvndvd cb9277d
Remove types for FileId
flvndvd caa6f15
:see_no_evil:
flvndvd d62ad30
Implement use cases for pre-processing
flvndvd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { | ||
FileUseCase, | ||
Result, | ||
SupportedFileContentType, | ||
} from "@dust-tt/types"; | ||
import { Err, Ok } from "@dust-tt/types"; | ||
import sharp from "sharp"; | ||
import { pipeline } from "stream/promises"; | ||
|
||
import type { Authenticator } from "@app/lib/auth"; | ||
import type { FileResource } from "@app/lib/resources/file_resource"; | ||
import logger from "@app/logger/logger"; | ||
|
||
const resizeAndUploadToFileStorage: PreprocessingFunction = async ( | ||
auth: Authenticator, | ||
file: FileResource | ||
) => { | ||
const readStream = file.getReadStream(auth, "original"); | ||
|
||
// Resize the image, preserving the aspect ratio. Longest side is max 768px. | ||
const resizedImageStream = sharp().resize(768, 768, { | ||
fit: sharp.fit.inside, // Ensure longest side is 768px. | ||
withoutEnlargement: true, // Avoid upscaling if image is smaller than 768px. | ||
}); | ||
|
||
const writeStream = file.getWriteStream(auth, "processed"); | ||
|
||
try { | ||
await pipeline(readStream, resizedImageStream, writeStream); | ||
|
||
return new Ok(undefined); | ||
} catch (err) { | ||
logger.error( | ||
{ | ||
fileId: file.sId, | ||
workspaceId: auth.workspace()?.sId, | ||
error: err, | ||
}, | ||
"Failed to resize image." | ||
); | ||
|
||
return new Err(err as Error); | ||
} | ||
}; | ||
|
||
// Preprocessing for file upload. | ||
|
||
type PreprocessingFunction = ( | ||
auth: Authenticator, | ||
file: FileResource | ||
) => Promise<Result<undefined, Error>>; | ||
|
||
type PreprocessingPerUseCase = { | ||
[k in FileUseCase]: PreprocessingFunction | undefined; | ||
}; | ||
|
||
type PreprocessingPerContentType = { | ||
[k in SupportedFileContentType]: PreprocessingPerUseCase | undefined; | ||
}; | ||
|
||
const processingPerContentType: Partial<PreprocessingPerContentType> = { | ||
"image/jpeg": { | ||
conversation: resizeAndUploadToFileStorage, | ||
}, | ||
"image/png": { | ||
conversation: resizeAndUploadToFileStorage, | ||
}, | ||
}; | ||
|
||
export async function maybeApplyPreProcessing( | ||
auth: Authenticator, | ||
file: FileResource | ||
): Promise<Result<undefined, Error>> { | ||
const contentTypeProcessing = processingPerContentType[file.contentType]; | ||
if (!contentTypeProcessing) { | ||
await file.markAsReady(); | ||
|
||
return new Ok(undefined); | ||
} | ||
|
||
const processing = contentTypeProcessing[file.useCase]; | ||
if (processing) { | ||
const res = await processing(auth, file); | ||
if (res.isErr()) { | ||
await file.markAsFailed(); | ||
|
||
return res; | ||
} | ||
} | ||
|
||
await file.markAsReady(); | ||
|
||
return new Ok(undefined); | ||
} |
This file contains 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 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 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 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.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored so delete always takes an
Authenticator
instance.