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

Metadata tags #1070

Merged
merged 53 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
616d6f3
added console.log
stevestencil Aug 8, 2019
4e693dd
added _batchCount and _batchIndex properties
stevestencil Aug 8, 2019
50aa5a8
test batch index and count
stevestencil Aug 9, 2019
9e51c56
Merge branch 'master' into master
stevestencil Aug 9, 2019
ffb9b11
Merge branch 'master' into master
stevestencil Aug 9, 2019
4275c29
added hint
stevestencil Dec 23, 2019
7d488b8
added hint to ParseQuery
stevestencil Jan 7, 2020
cd612b9
fixed failed tests
stevestencil Jan 7, 2020
7efb326
removed _batchIndex and _batchCount
stevestencil Jan 7, 2020
b28ac39
Merge pull request #2 from parse-community/master
stevestencil Jan 8, 2020
dcbcc4d
Merge branch 'master' into query-hint
stevestencil Jan 8, 2020
773601c
added documentation and support for chaining
stevestencil Jan 8, 2020
1827459
added documentation and tests
stevestencil Jan 8, 2020
edd9c42
Merge branch 'master' into query-hint
dplewis Jan 14, 2020
6b51be9
Merge pull request #4 from parse-community/master
stevestencil Jan 17, 2020
81088cd
added support for metadata and tags
stevestencil Jan 17, 2020
5a44266
added more docs
stevestencil Jan 17, 2020
47e462e
removed validation for values
stevestencil Jan 17, 2020
da5fa5b
added docs for beforeSaveFile and afterSaveFile
stevestencil Jan 18, 2020
312175d
updated docs
stevestencil Jan 18, 2020
483e4bc
Merge branch 'master' of https://github.com/stevestencil/Parse-SDK-JS
stevestencil Jan 18, 2020
b35b677
Merge branch 'master' of https://github.com/parse-community/Parse-SDK…
stevestencil Jan 18, 2020
720be0d
Merge branch 'query-hint'
stevestencil Jan 18, 2020
318edaf
added delete file route
stevestencil Jan 18, 2020
0d2df40
Merge branch 'master' into metadata-tags
dplewis Jan 19, 2020
23106c4
add getters
dplewis Jan 19, 2020
7a8b66d
Merge branch 'metadata-tags' of https://github.com/stevestencil/Parse…
dplewis Jan 19, 2020
4994edb
clean up
dplewis Jan 19, 2020
00ab4ef
Merge branch 'master' of https://github.com/parse-community/Parse-SDK…
stevestencil Jan 19, 2020
41cb11f
Merge branch 'file-delete' into metadata-tags
stevestencil Jan 19, 2020
3d13de6
Merge branch 'master' of https://github.com/parse-community/Parse-SDK-JS
stevestencil Jan 19, 2020
8496b7c
added decrement function to ParseObject
stevestencil Jan 19, 2020
79a679d
Merge branch 'metadata-tags' into develop
stevestencil Jan 21, 2020
8cfcbbf
udpated scope
stevestencil Jan 21, 2020
908536c
reverted package.json
stevestencil Jan 21, 2020
2b83cf1
Merge branch 'master' into master
stevestencil Jan 21, 2020
0eccd83
Merge branch 'master' of https://github.com/stevestencil/Parse-SDK-JS…
stevestencil Jan 22, 2020
2ef2153
added metadata and tags to save request
stevestencil Jan 22, 2020
414d0b7
Merge branch 'master' into metadata-tags
stevestencil Jan 22, 2020
cf5e1c5
fixed lint fail
stevestencil Jan 22, 2020
f8d4f9e
Merge branch 'master' of https://github.com/stevestencil/Parse-SDK-JS…
stevestencil Jan 22, 2020
c4187cd
Merge branch 'metadata-tags' of https://github.com/stevestencil/Parse…
stevestencil Jan 22, 2020
416c283
Merge branch 'metadata-tags' of https://bitbucket.org/leapllc/parse-s…
stevestencil Jan 26, 2020
25487ea
Merge branch 'master' into metadata-tags
stevestencil Jan 26, 2020
d10c662
Merge branch 'master' into metadata-tags
stevestencil Mar 4, 2020
fa46ca9
Merge branch 'master' into metadata-tags
dplewis Mar 6, 2020
f5fb7e5
Merge branch 'master' into metadata-tags
stevestencil Mar 6, 2020
ef3bc1c
Merge branch 'master' into metadata-tags
dplewis Mar 22, 2020
1ac6959
remove unnecessary code
stevestencil Mar 22, 2020
1845254
fixed tests
stevestencil Mar 29, 2020
ee42dda
Merge branch 'master' into metadata-tags
dplewis Mar 29, 2020
d1ab174
fixed linting errors
stevestencil Mar 29, 2020
9791906
Merge branch 'metadata-tags' of https://github.com/stevestencil/Parse…
stevestencil Mar 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions src/ParseFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,39 +418,45 @@ const DefaultController = {
if (source.format !== 'file') {
throw new Error('saveFile can only be used with File-type sources.');
}
// To directly upload a File, we use a REST-style AJAX request
const headers = {
'X-Parse-Application-ID': CoreManager.get('APPLICATION_ID'),
'Content-Type': source.type || (source.file ? source.file.type : null)
};
const jsKey = CoreManager.get('JAVASCRIPT_KEY');
if (jsKey) {
headers['X-Parse-JavaScript-Key'] = jsKey;
}
let sessionToken = options.sessionToken;
const userController = CoreManager.getUserController();
if (!sessionToken && userController) {
const currentUser = await userController.currentUserAsync();
sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
}
if (sessionToken) {
headers['X-Parse-Session-Token'] = sessionToken;
}
let url = CoreManager.get('SERVER_URL');
if (url[url.length - 1] !== '/') {
url += '/';
if (source.base64) {
dplewis marked this conversation as resolved.
Show resolved Hide resolved
// If base64 data has already been created, go ahead and save it.
source.format = 'base64';
return await DefaultController.saveBase64(name, source, options);
}
url += 'files/' + name;
return CoreManager.getRESTController().ajax('POST', url, source.file, headers, options).then(res=>res.response)
const base64Data = await new Promise((res, rej) => {
// eslint-disable-next-line no-undef
const reader = new FileReader();
reader.readAsDataURL(source.file);
reader.onload = () => res(reader.result);
reader.onerror = error => rej(error);
});
// we only want the data after the comma
// For example: "data:application/pdf;base64,JVBERi0xLjQKJ..." we would only want "JVBERi0xLjQKJ..."
const [first, second] = base64Data.split(',');
// in the event there is no 'data:application/pdf;base64,' at the beginning of the base64 string
// use the entire string instead
const data = second ? second : first;
const newSource = {
format: 'base64',
base64: data,
type: source.type || (source.file ? source.file.type : null),
};
return await DefaultController.saveBase64(name, newSource, options);
},

saveBase64: function(name: string, source: FileSource, options?: FullOptions) {
if (source.format !== 'base64') {
throw new Error('saveBase64 can only be used with Base64-type sources.');
}
const data: { base64: any; _ContentType?: any } = {
base64: source.base64
const data: { base64: any; _ContentType?: any, fileData: Object } = {
base64: source.base64,
fileData: {
metadata: { ...options.metadata },
tags: { ...options.tags },
},
};
delete options.metadata;
delete options.tags;
if (source.type) {
data._ContentType = source.type;
}
Expand Down
66 changes: 60 additions & 6 deletions src/__tests__/ParseFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ describe('FileController', () => {

return file.save().then(function(f) {
expect(f).toBe(file);
expect(f.name()).toBe('/api.parse.com/1/files/parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a//api.parse.com/1/files/parse.txt');
expect(f.name()).toBe('parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a/parse.txt');
});
});

Expand Down Expand Up @@ -652,8 +652,8 @@ describe('FileController', () => {

return file.save({ sessionToken: 'testing_sessionToken' }).then(function(f) {
expect(f).toBe(file);
expect(f.name()).toBe('/api.parse.com/1/files/parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a//api.parse.com/1/files/parse.txt');
expect(f.name()).toBe('parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a/parse.txt');
});
});

Expand Down Expand Up @@ -690,9 +690,63 @@ describe('FileController', () => {

return file.save().then(function(f) {
expect(f).toBe(file);
expect(f.name()).toBe('/api.parse.com/1/files/parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a//api.parse.com/1/files/parse.txt');
expect(f.name()).toBe('parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a/parse.txt');
});
});

it('should save file using saveFile with metadata and tags', async () => {
CoreManager.set('UserController', {
currentUserAsync() {
return Promise.resolve({
getSessionToken() {
return 'currentUserToken';
}
});
}
});
const request = jest.fn((method, path) => {
const name = path.substr(path.indexOf('/') + 1);
return Promise.resolve({
name: name,
url: 'https://files.parsetfss.com/a/' + name
});
});
const ajax = function(method, path, data, headers) {
expect(headers['X-Parse-Session-Token']).toBe('currentUserToken')
const name = path.substr(path.indexOf('/') + 1);
return Promise.resolve({
response: {
name: name,
url: 'https://files.parsetfss.com/a/' + name
}
});
};
CoreManager.setRESTController({ request, ajax });
const file = new ParseFile('parse.txt', [61, 170, 236, 120]);
file._source.format = 'file';
file.addMetadata('foo', 'bar');
file.addTag('bar', 'foo');
const f = await file.save();
expect(f).toBe(file);
expect(f.name()).toBe('parse.txt');
expect(f.url()).toBe('https://files.parsetfss.com/a/parse.txt');
expect(request).toHaveBeenCalledWith(
'POST',
'files/parse.txt',
{
base64: 'ParseA==',
fileData: {
metadata: {
foo: 'bar',
},
tags: {
bar: 'foo',
},
},
},
{ requestTask: expect.any(Function) },
);
});

it('saves files via object saveAll options', async () => {
Expand Down