Skip to content
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

Cannot get the size of a file upload without buffering the entire file #53

Closed
hakimLyon opened this issue Feb 5, 2025 · 3 comments
Closed

Comments

@hakimLyon
Copy link

hakimLyon commented Feb 5, 2025

Hello,

After upgrading to @mjackson/file-storage version 0.6.0, I'm encountering an issue when attempting to upload files. The error occurs during the file upload process.

Image
@Apsysikal
Copy link

I also have this issue. I can see that size gets accessed in the local-file-storage.ts which probably throws the error.

let meta: FileMetadata = {
  key,
  lastModified: file.lastModified,
  name: file.name,
  size: file.size,
  type: file.type,
};

I'm not sure though if it's needed. I couldn't figure out what exactly is happening with the stringified parameter.

await fsp.writeFile(metaPath, JSON.stringify(meta));

@lasseklovstad
Copy link

I also have the same issue. A hack to make it work is to override the getter on FileUpload class before until its fixed:

function createTempUploadHandler(prefix: string) {
  const directory = path.join(os.tmpdir(), prefix);
  const fileStorage = new LocalFileStorage(directory);

  async function uploadHandler(fileUpload: FileUpload) {
    if (fileUpload.fieldName === "media") {
      const key = new Date().getTime().toString(36);
      Object.defineProperty(fileUpload, "size", {
        get: () => 0,
      });
      await fileStorage.set(key, fileUpload);
      return fileStorage.get(key);
    }

    // Ignore any files we don't recognize the name of...
  }
  return uploadHandler;
}

@mjackson
Copy link
Owner

mjackson commented Feb 6, 2025

Sorry about the regression! This is fixed and released in [email protected]

I also added a few integration tests in f1063ac that should help prevent this regression in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants