Skip to content

Commit

Permalink
fix(bigFileUpload): export processBigFileUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Sep 12, 2024
1 parent 1ad0e16 commit d5b87d3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/forms/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ThrowOverrideResponse from "./dist/throw-action/throwOverrideResponse.js"
import UploadBigFile from './dist/components/form/UploadBigFile/UploadBigFile.astro';
import UploadBigFileProgress from './dist/components/form/UploadBigFile/UploadBigFileProgress.astro';
import {BigFile} from './dist/components/form/UploadBigFile/BigFile.js';
import { processBigFileUpload } from './dist/components/form/UploadBigFile/uploadBigFileServer.js';

export {
Bind,
Expand All @@ -25,7 +26,8 @@ export {
BigFile,
UploadBigFile,
UploadBigFileProgress,
ThrowOverrideResponse
ThrowOverrideResponse,
processBigFileUpload
}

export type {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
---
import type { ComponentProps } from 'astro/types';
import ThrowOverrideResponse from '../../../throw-action/throwOverrideResponse.js';
import BInput from '../BInput.astro';
import { loadUploadFiles } from './uploadBigFileServer.js';
import { processBigFileUpload } from './uploadBigFileServer.js';
export interface Props<T extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>> extends ComponentProps<typeof BInput<T>> {
}
const { class: className, tempDirectory, name, ...props } = Astro.props;
const bigFileServerOptions = Astro.locals.__formsInternalUtils.FORM_OPTIONS.forms?.bigFilesUpload?.bigFileServerOptions;
const uploadFilesResponse = await loadUploadFiles(Astro, bigFileServerOptions);
if (uploadFilesResponse) {
throw new ThrowOverrideResponse(uploadFilesResponse);
}
await processBigFileUpload(Astro);
---

<BInput type='file' {...props} name={name} class:list={[className, 'big-upload']} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import os from "os";
import { validateFrom } from "../../../form-tools/csrf.js";
import { AstroGlobal } from "astro";
import { getFormValue } from "../../../form-tools/post.js";
import ThrowOverrideResponse from "src/throw-action/throwOverrideResponse.js";

const zodValidationInfo =
z.preprocess((str: any, ctx) => {
Expand Down Expand Up @@ -42,7 +43,7 @@ export const DEFAULT_BIG_FILE_UPLOAD_OPTIONS_SERVER: LoadUploadFilesOptions = {
tempDirectory: path.join(os.tmpdir(), "astro_forms_big_files_uploads"),
};

export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadUploadFilesOptions> = {}) {
async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadUploadFilesOptions> = {}) {
const { allowUpload, onFinished, maxUploadTime, maxUploadSize, maxDirectorySize, tempDirectory } = { ...DEFAULT_BIG_FILE_UPLOAD_OPTIONS_SERVER, ...options };
if (astro.request.method !== "POST" || !await validateFrom(astro)) {
return false;
Expand Down Expand Up @@ -94,7 +95,7 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
return await sendError("Directory size exceeded");
}

const newTotalSize = (await totalDirectorySize(uploadDir)) + uploadFileMayBe.size;
const newTotalSize = (await totalDirectorySize(uploadDir)) + uploadFile.size;
if (newTotalSize > maxUploadSize) {
return await sendError("Upload size exceeded");
}
Expand Down Expand Up @@ -145,6 +146,13 @@ export async function loadUploadFiles(astro: AstroGlobal, options: Partial<LoadU
return Response.json({ ok: true, finished: true });
}

export async function processBigFileUpload(astro: AstroGlobal, options: Partial<LoadUploadFilesOptions> = astro.locals.__formsInternalUtils.FORM_OPTIONS.forms?.bigFilesUpload?.bigFileServerOptions) {
const haveFileUpload = await loadUploadFiles(astro, options);
if(haveFileUpload) {
throw new ThrowOverrideResponse(haveFileUpload);
}
}

async function deleteOldUploads(tempDirectory: string, maxUploadTime: number) {
const files = await fs.readdir(tempDirectory);
for (const file of files) {
Expand Down

0 comments on commit d5b87d3

Please sign in to comment.