yakovkhalinsky / backblaze-b2

Node.js Library for the Backblaze B2 Storage Service
MIT License
191 stars 59 forks source link

Add "fileInfo" argument to startLargeFile #139

Open tapetkabinett opened 4 months ago

tapetkabinett commented 4 months ago

In the Backblaze B2 docs we can see that the b2_start_large_file endpoint has a "fileInfo" argument. Without this argument, we cannot set headers such as Content-Disposition on the uploaded file, which makes it impossible to specify that we want the file to be an attachment.

I'm requesting the following changes be made to the backblaze-b2 package: lib>actions>file.js

  1. Change the following code:

    exports.startLargeFile = function(b2, args) {
    const options = {
        url: endpoints(b2).startLargeFileUrl,
        method: 'POST',
        headers: utils.getAuthHeaderObjectWithToken(b2),
        data: {
            bucketId: args.bucketId,
            fileName: args.fileName,
            contentType: args.contentType || 'b2/x-auto'
        }
    };
    
    // merge order matters here: later objects override earlier objects
    return request.sendRequest(_.merge({},
        _.get(args, 'axios', {}),
        options,
        _.get(args, 'axiosOverride', {})
    ));
    };

    to

    exports.startLargeFile = function (b2, args) {
    const options = {
    url: endpoints(b2).startLargeFileUrl,
    method: 'POST',
    headers: utils.getAuthHeaderObjectWithToken(b2),
    data: {
      bucketId: args.bucketId,
      fileName: args.fileName,
      fileInfo: args.fileInfo,
      contentType: args.contentType || 'b2/x-auto',
    },
    };
    
    // merge order matters here: later objects override earlier objects
    return request.sendRequest(
    _.merge(
      {},
      _.get(args, 'axios', {}),
      options,
      _.get(args, 'axiosOverride', {})
    )
    );
    };

And the following changes be made to the @types/backblaze-b2 package: index.d.ts

  1. Add one of the following types:
    interface StartLargeFileOpts extends CommonArgs {
    bucketId: string;
    fileName: string;
    fileInfo?: {
        "b2-content-disposition"?: string;
        "b2-content-language"?: string;
        "b2-expires"?: string;
        "b2-cache-control"?: string;
        "b2-content-encoding"?: string;
    } | undefined;
    }

    or

    interface StartLargeFileOpts extends CommonArgs {
    bucketId: string;
    fileName: string;
    fileInfo?: Record<string, any> | undefined;
    }
  2. Change the following type: startLargeFile(opts: { bucketId: string; fileName: string } & CommonArgs): Promise<StandardApiResponse>; to startLargeFile(opts: StartLargeFileOpts): Promise<StandardApiResponse>;