pqina / vue-filepond

🔌 A handy FilePond adapter component for Vue
https://pqina.nl/filepond
MIT License
1.92k stars 128 forks source link

Process: Custom-Headers not applied, when Filesize < Chunk-Size #193

Closed BuchholzTim closed 3 years ago

BuchholzTim commented 3 years ago

Describe the bug: Currently Filepond does not send the filename on the first Process-Request, if it's uploading a file, which has a filesize greater than chunk-size. I also need to apply two additonal headers to the requests (projectId, Authorization).

Those two headers are easily applied by setting the server-Property to the following computed-Value:

computed: {
    headers() {
      return {
        Authorization: this.$auth.getToken('local'),
        projectId: this.projectId,
      };
    },
    server() {
      return {
        url: 'http://localhost:3001/api/filepond/',
        process: {
          url: 'process',
          headers: this.headers,
        },
        patch: {
          url: 'patch?id=',
          headers: this.headers,
        },
      };
    },
}

This applied my two custom headers additonally to everything filepond does on its own.

I tried setting the filename in the Process-Request by passing a Function instead of an Object to the server.process.headers-Value, as described in this Issue.

The new code looks like this:

  computed: {
    server() {
      return {
        url: 'http://localhost:3001/api/filepond/',
        process: {
          url: 'process',
          headers: (file, metaData) => this.processHeaders(file, metaData),
        },
        patch: {
          url: 'patch?id=',
          headers: (file, metaData) => this.headers(file, metaData),
        },
      };
    },
  },
  methods: {
    processHeaders(file, metaData) {
      // Log some stuff on Process - Irrelevant for this issue
      return this.headers(file, metaData);
    },
    headers(file, metaData) {
      return {
        Authorization: this.$auth.getToken('local'),
        projectId: this.projectId,
        'Upload-Length': file.file ? file.file.size : file.size,
        'Upload-Name': file.name ? file.name : file.file.name,
        'Upload-Offset': file.file ? file.offset : undefined,
      };
    },
  }

This Code works perfectly fine for any filesize greater than chunk-size. The first Process-Request receives all the necessary headers and all Patch-Requests afterwards also receive them.

If the file however can be uploaded in one request (i.e. filesize < chunk-size), then the Process-Request does not receive my headers and in consequence is not authorized to upload to the backend.

In fact, the processHeaders(file, metaData)-Function only gets called, when a Patch-Request will follow. On a single Process-Request, it won't even call the method.

Information about your project:

rikschennink commented 3 years ago

Thanks for the detailed issue. I wonder if chunkForce would fix this?

BuchholzTim commented 3 years ago

Thanks for the reply and the nice workaround. Setting chunkForce fixes my particular problem. Perhaps I overlooked it in the documentation.

rikschennink commented 3 years ago

Glad to hear that :)

I'll close the issue for you.

akislawek commented 2 years ago

@rikschennink I came across the same issue as @BuchholzTim, and found this behaviour confusing, especially that the docs don't mention this in the "Server configuration" section (https://pqina.nl/filepond/docs/api/server/#process-chunks), leaving me think that once I set chunkUploads to true (as mentioned in the docs) all files would follow the same process.

Perhaps adding a comment regarding this (and the chunkForce option) in the "process chunks" section of the docs would save people time in the future (it would surely save me time not having to code a workaround for this :)

thanks for the great plugin, by the way!

rikschennink commented 2 years ago

Good point. I've added an additional entry to the server docs.