feat: Store image uploads as attachments in database (#1144)

* First pass

* Documentation

* Added optional documentId relationship

* name -> key

* cleanup: No need for separate documentId prop
This commit is contained in:
Tom Moor
2020-01-16 09:42:42 -08:00
committed by GitHub
parent 22230c25e5
commit 8e5a5a57a9
8 changed files with 171 additions and 31 deletions

View File

@ -4,17 +4,19 @@ import invariant from 'invariant';
type Options = {
name?: string,
documentId?: string,
};
export const uploadFile = async (
file: File | Blob,
option?: Options = { name: '' }
options?: Options = { name: '' }
) => {
const filename = file instanceof File ? file.name : option.name;
const name = file instanceof File ? file.name : options.name;
const response = await client.post('/users.s3Upload', {
kind: file.type,
documentId: options.documentId,
contentType: file.type,
size: file.size,
filename,
name,
});
invariant(response, 'Response should be available');
@ -35,11 +37,10 @@ export const uploadFile = async (
formData.append('file', file);
}
const options: Object = {
await fetch(data.uploadUrl, {
method: 'post',
body: formData,
};
await fetch(data.uploadUrl, options);
});
return asset;
};