Skip to content

Commit

Permalink
Feat: Add MEDIA_UPLOAD_CHUNK_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
Vija02 committed Jan 24, 2025
1 parent 2dcebf4 commit 80fed38
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
9 changes: 5 additions & 4 deletions backend/server/src/middleware/installRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ function transformer(html: string, req: Request) {

// Extra data
const extraEnv = [
{ ROOT_URL: process.env.ROOT_URL, CSRF_TOKEN: req.csrfToken() } as Record<
string,
string
>,
{
ROOT_URL: process.env.ROOT_URL,
CSRF_TOKEN: req.csrfToken(),
MEDIA_UPLOAD_CHUNK_SIZE: process.env.MEDIA_UPLOAD_CHUNK_SIZE,
} as Record<string, string>,
]
.concat(registeredEnvToViews.map((x) => x.envVars))
.reduce((acc, val) => ({ ...acc, ...val }), {} as Record<string, string>);
Expand Down
9 changes: 5 additions & 4 deletions backend/server/src/middleware/installRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ function transformer(html: string, req: Request) {

// Extra data
const extraEnv = [
{ ROOT_URL: process.env.ROOT_URL, CSRF_TOKEN: req.csrfToken() } as Record<
string,
string
>,
{
ROOT_URL: process.env.ROOT_URL,
CSRF_TOKEN: req.csrfToken(),
MEDIA_UPLOAD_CHUNK_SIZE: process.env.MEDIA_UPLOAD_CHUNK_SIZE,
} as Record<string, string>,
]
.concat(registeredEnvToViews.map((x) => x.envVars))
.reduce((acc, val) => ({ ...acc, ...val }), {} as Record<string, string>);
Expand Down
17 changes: 16 additions & 1 deletion packages/lib/src/appData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
const getRootURL = () => (window as any)?.__APP_DATA__?.ROOT_URL;
const getCSRFToken = () => (window as any)?.__APP_DATA__?.CSRF_TOKEN;
const getMediaUploadChunkSize = () => {
const val = parseInt(
(window as any)?.__APP_DATA__?.MEDIA_UPLOAD_CHUNK_SIZE,
10,
);
if (Number.isSafeInteger(val)) {
return val;
}
return Infinity;
};

const getCustomEnv = (envName: string) =>
(window as any)?.__APP_DATA__?.[envName];

export const appData = { getRootURL, getCSRFToken, getCustomEnv };
export const appData = {
getRootURL,
getCSRFToken,
getMediaUploadChunkSize,
getCustomEnv,
};
2 changes: 1 addition & 1 deletion plugins/audio-recorder/view/useAudioRecording.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function startUpload(
) {
const endpoint = pluginApi.media.tusUploadUrl;
// DEBT: Maybe need bigger chunkSize
const chunkSize = 15000; // 15kb. Roughly every second
const chunkSize = Math.min(15000, pluginApi.env.getMediaUploadChunkSize()); // 15kb. Roughly every second

const options: ConstructorParameters<typeof tus.Upload>[1] = {
endpoint,
Expand Down
1 change: 1 addition & 0 deletions plugins/simple-image/view/ImageRemote/UploadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const UploadModal = () => {
"csrf-token": appData.getCSRFToken(),
"organization-id": pluginApi.pluginContext.organizationId,
},
chunkSize: pluginApi.env.getMediaUploadChunkSize(),
}),
);
const sceneData = pluginApi.scene.useValtioData();
Expand Down
2 changes: 2 additions & 0 deletions plugins/video-player/view/Remote/UploadVideoModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ const UploadVideoModal = ({
...props
}: UploadVideoModalPropTypes) => {
const pluginApi = usePluginAPI();

const [uppy] = useState(() =>
new Uppy().use(Tus, {
endpoint: pluginApi.media.tusUploadUrl,
headers: {
"csrf-token": appData.getCSRFToken(),
"organization-id": pluginApi.pluginContext.organizationId,
},
chunkSize: pluginApi.env.getMediaUploadChunkSize(),
}),
);
const mutableSceneData = pluginApi.scene.useValtioData();
Expand Down
8 changes: 8 additions & 0 deletions scripts/_setup_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ exports.updateDotenv = function updateDotenv(add, answers) {
# For any other values, this option is disabled`,
);

add(
"MEDIA_UPLOAD_CHUNK_SIZE",
"100000000",
`\
# How big each chunk of a file upload should be. This is useful in environments where there is a limit to how big a file upload can be.
# Big files will be chunked into the provided size. Default is 100000000 which is 100mb`,
);

add(
"VITE_APP_OPENOBSERVE_CLIENT_TOKEN",
`\
Expand Down

0 comments on commit 80fed38

Please sign in to comment.