Skip to content

Commit

Permalink
fix: empty file tags cause upload error for some providers (#7300)
Browse files Browse the repository at this point in the history
* fix: empty file tags cause upload error for some providers

DigitalOcean and Linode object storage solutions do not accept `tags` option while uploading a file. Previously, tags option was set to default empty object. Now, we do not include it if it is empty.

* chore: add tests for saving a file with/without tags

* chore: update file tags handling to make tests pass

* chore: refactor file tag tests

* chore: update file tag tests

* chore: update changelog

* chore: update changelog entry

* chore: remove duplicated changelog entry
  • Loading branch information
alioguzhan authored Apr 2, 2021
1 parent 626352d commit 4d16702
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ ___
- Excluding keys that have trailing edges.node when performing GraphQL resolver (Chris Bland) [#7273](https://github.com/parse-community/parse-server/pull/7273)
- Added centralized feature deprecation with standardized warning logs (Manuel Trezza) [#7303](https://github.com/parse-community/parse-server/pull/7303)
- Use Node.js 15.13.0 in CI (Olle Jonsson) [#7312](https://github.com/parse-community/parse-server/pull/7312)
- Fix file upload issue for S3 compatible storage (Linode, DigitalOcean) by avoiding empty tags property when creating a file (Ali Oguzhan Yildiz) [#7300](https://github.com/parse-community/parse-server/pull/7300)
___
## 4.5.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)
Expand Down
29 changes: 29 additions & 0 deletions spec/ParseFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

'use strict';

const { FilesController } = require('../lib/Controllers/FilesController');
const request = require('../lib/request');

const str = 'Hello World!';
Expand Down Expand Up @@ -205,6 +206,34 @@ describe('Parse.File testing', () => {
notEqual(file.name(), 'hello.txt');
});

it('saves the file with tags', async () => {
spyOn(FilesController.prototype, 'createFile').and.callThrough();
const file = new Parse.File('hello.txt', data, 'text/plain');
const tags = { hello: 'world' };
file.setTags(tags);
expect(file.url()).toBeUndefined();
const result = await file.save();
expect(file.name()).toBeDefined();
expect(file.url()).toBeDefined();
expect(result.tags()).toEqual(tags);
expect(FilesController.prototype.createFile.calls.argsFor(0)[4]).toEqual({
tags: tags,
metadata: {},
});
});

it('does not pass empty file tags while saving', async () => {
spyOn(FilesController.prototype, 'createFile').and.callThrough();
const file = new Parse.File('hello.txt', data, 'text/plain');
expect(file.url()).toBeUndefined();
expect(file.name()).toBeDefined();
await file.save();
expect(file.url()).toBeDefined();
expect(FilesController.prototype.createFile.calls.argsFor(0)[4]).toEqual({
metadata: {},
});
});

it('save file in object', async done => {
const file = new Parse.File('hello.txt', data, 'text/plain');
ok(!file.url());
Expand Down
14 changes: 10 additions & 4 deletions src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,22 @@ export class FilesRouter {
// update fileSize
const bufferData = Buffer.from(fileObject.file._data, 'base64');
fileObject.fileSize = Buffer.byteLength(bufferData);
// prepare file options
const fileOptions = {
metadata: fileObject.file._metadata,
};
// some s3-compatible providers (DigitalOcean, Linode) do not accept tags
// so we do not include the tags option if it is empty.
const fileTags =
Object.keys(fileObject.file._tags).length > 0 ? { tags: fileObject.file._tags } : {};
Object.assign(fileOptions, fileTags);
// save file
const createFileResult = await filesController.createFile(
config,
fileObject.file._name,
bufferData,
fileObject.file._source.type,
{
tags: fileObject.file._tags,
metadata: fileObject.file._metadata,
}
fileOptions
);
// update file with new data
fileObject.file._name = createFileResult.name;
Expand Down

0 comments on commit 4d16702

Please sign in to comment.