GraphQL: Nested File Upload (#6372)
* wip * wip * tested * wip * tested
This commit is contained in:
committed by
Antonio Davi Macedo Coelho de Castro
parent
df3fa029bc
commit
30a5aa0b61
@@ -366,6 +366,20 @@ const FILE_INFO = new GraphQLObjectType({
|
||||
},
|
||||
});
|
||||
|
||||
const FILE_INPUT = new GraphQLInputObjectType({
|
||||
name: 'FileInput',
|
||||
fields: {
|
||||
file: {
|
||||
description: 'A File Scalar can be an url or a FileInfo object.',
|
||||
type: FILE,
|
||||
},
|
||||
upload: {
|
||||
description: 'Use this field if you want to create a new file.',
|
||||
type: GraphQLUpload,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const GEO_POINT_FIELDS = {
|
||||
latitude: {
|
||||
description: 'This is the latitude.',
|
||||
@@ -1244,6 +1258,7 @@ const load = parseGraphQLSchema => {
|
||||
parseGraphQLSchema.addGraphQLType(BYTES, true);
|
||||
parseGraphQLSchema.addGraphQLType(FILE, true);
|
||||
parseGraphQLSchema.addGraphQLType(FILE_INFO, true);
|
||||
parseGraphQLSchema.addGraphQLType(FILE_INPUT, true);
|
||||
parseGraphQLSchema.addGraphQLType(GEO_POINT_INPUT, true);
|
||||
parseGraphQLSchema.addGraphQLType(GEO_POINT, true);
|
||||
parseGraphQLSchema.addGraphQLType(PARSE_OBJECT, true);
|
||||
@@ -1301,6 +1316,7 @@ export {
|
||||
SELECT_INPUT,
|
||||
FILE,
|
||||
FILE_INFO,
|
||||
FILE_INPUT,
|
||||
GEO_POINT_FIELDS,
|
||||
GEO_POINT_INPUT,
|
||||
GEO_POINT,
|
||||
|
||||
@@ -5,6 +5,53 @@ import Parse from 'parse/node';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
import logger from '../../logger';
|
||||
|
||||
const handleUpload = async (upload, config) => {
|
||||
const { createReadStream, filename, mimetype } = await upload;
|
||||
let data = null;
|
||||
if (createReadStream) {
|
||||
const stream = createReadStream();
|
||||
data = await new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
stream
|
||||
.on('error', reject)
|
||||
.on('data', chunk => chunks.push(chunk))
|
||||
.on('end', () => resolve(Buffer.concat(chunks)));
|
||||
});
|
||||
}
|
||||
|
||||
if (!data || !data.length) {
|
||||
throw new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Invalid file upload.');
|
||||
}
|
||||
|
||||
if (filename.length > 128) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename too long.');
|
||||
}
|
||||
|
||||
if (!filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_FILE_NAME,
|
||||
'Filename contains invalid characters.'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return {
|
||||
fileInfo: await config.filesController.createFile(
|
||||
config,
|
||||
filename,
|
||||
data,
|
||||
mimetype
|
||||
),
|
||||
};
|
||||
} catch (e) {
|
||||
logger.error('Error creating a file: ', e);
|
||||
throw new Parse.Error(
|
||||
Parse.Error.FILE_SAVE_ERROR,
|
||||
`Could not store file: ${filename}.`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const load = parseGraphQLSchema => {
|
||||
const createMutation = mutationWithClientMutationId({
|
||||
name: 'CreateFile',
|
||||
@@ -26,57 +73,7 @@ const load = parseGraphQLSchema => {
|
||||
try {
|
||||
const { upload } = args;
|
||||
const { config } = context;
|
||||
|
||||
const { createReadStream, filename, mimetype } = await upload;
|
||||
let data = null;
|
||||
if (createReadStream) {
|
||||
const stream = createReadStream();
|
||||
data = await new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
stream
|
||||
.on('error', reject)
|
||||
.on('data', chunk => chunks.push(chunk))
|
||||
.on('end', () => resolve(Buffer.concat(chunks)));
|
||||
});
|
||||
}
|
||||
|
||||
if (!data || !data.length) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.FILE_SAVE_ERROR,
|
||||
'Invalid file upload.'
|
||||
);
|
||||
}
|
||||
|
||||
if (filename.length > 128) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_FILE_NAME,
|
||||
'Filename too long.'
|
||||
);
|
||||
}
|
||||
|
||||
if (!filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_FILE_NAME,
|
||||
'Filename contains invalid characters.'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return {
|
||||
fileInfo: await config.filesController.createFile(
|
||||
config,
|
||||
filename,
|
||||
data,
|
||||
mimetype
|
||||
),
|
||||
};
|
||||
} catch (e) {
|
||||
logger.error('Error creating a file: ', e);
|
||||
throw new Parse.Error(
|
||||
Parse.Error.FILE_SAVE_ERROR,
|
||||
`Could not store file: ${filename}.`
|
||||
);
|
||||
}
|
||||
return handleUpload(upload, config);
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -97,4 +94,4 @@ const load = parseGraphQLSchema => {
|
||||
);
|
||||
};
|
||||
|
||||
export { load };
|
||||
export { load, handleUpload };
|
||||
|
||||
Reference in New Issue
Block a user