Skip to content

Commit

Permalink
feat: reorders uploads to provide beforeChange hooks with upload data
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Mar 13, 2021
1 parent 71a15b5 commit 3c42e6e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 120 deletions.
108 changes: 54 additions & 54 deletions src/collections/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,59 @@ async function create(this: Payload, incomingArgs: Arguments): Promise<Document>
await executeAccess({ req }, collectionConfig.access.create);
}

// /////////////////////////////////////
// Upload and resize potential files
// /////////////////////////////////////

if (collectionConfig.upload) {
const fileData: Partial<FileData> = {};

const { staticDir, imageSizes } = collectionConfig.upload;

const file = ((req.files && req.files.file) ? req.files.file : req.file) as UploadedFile;

if (!file) {
throw new MissingFile();
}

let staticPath = staticDir;

if (staticDir.indexOf('/') !== 0) {
staticPath = path.join(config.paths.configDir, staticDir);
}

mkdirp.sync(staticPath);

const fsSafeName = await getSafeFilename(staticPath, file.name);

try {
await saveBufferToFile(file.data, `${staticPath}/${fsSafeName}`);

if (isImage(file.mimetype)) {
const dimensions = await getImageSize(`${staticPath}/${fsSafeName}`);
fileData.width = dimensions.width;
fileData.height = dimensions.height;

if (Array.isArray(imageSizes) && file.mimetype !== 'image/svg+xml') {
fileData.sizes = await resizeAndSave(staticPath, collectionConfig, fsSafeName, fileData.mimeType);
}
}
} catch (err) {
console.error(err);
throw new FileUploadError();
}


fileData.filename = fsSafeName;
fileData.filesize = file.size;
fileData.mimeType = file.mimetype;

data = {
...data,
...fileData,
};
}

// /////////////////////////////////////
// beforeValidate - Fields
// /////////////////////////////////////
Expand Down Expand Up @@ -114,7 +167,7 @@ async function create(this: Payload, incomingArgs: Arguments): Promise<Document>
// beforeChange - Fields
// /////////////////////////////////////

let resultWithLocales = await this.performFieldOperations(collectionConfig, {
const resultWithLocales = await this.performFieldOperations(collectionConfig, {
data,
hook: 'beforeChange',
operation: 'create',
Expand All @@ -123,59 +176,6 @@ async function create(this: Payload, incomingArgs: Arguments): Promise<Document>
unflattenLocales: true,
});

// /////////////////////////////////////
// Upload and resize potential files
// /////////////////////////////////////

if (collectionConfig.upload) {
const fileData: Partial<FileData> = {};

const { staticDir, imageSizes } = collectionConfig.upload;

const file = ((req.files && req.files.file) ? req.files.file : req.file) as UploadedFile;

if (!file) {
throw new MissingFile();
}

let staticPath = staticDir;

if (staticDir.indexOf('/') !== 0) {
staticPath = path.join(config.paths.configDir, staticDir);
}

mkdirp.sync(staticPath);

const fsSafeName = await getSafeFilename(staticPath, file.name);

try {
await saveBufferToFile(file.data, `${staticPath}/${fsSafeName}`);

if (isImage(file.mimetype)) {
const dimensions = await getImageSize(`${staticPath}/${fsSafeName}`);
fileData.width = dimensions.width;
fileData.height = dimensions.height;

if (Array.isArray(imageSizes) && file.mimetype !== 'image/svg+xml') {
fileData.sizes = await resizeAndSave(staticPath, collectionConfig, fsSafeName, fileData.mimeType);
}
}
} catch (err) {
console.error(err);
throw new FileUploadError();
}


fileData.filename = fsSafeName;
fileData.filesize = file.size;
fileData.mimeType = file.mimetype;

resultWithLocales = {
...resultWithLocales,
...fileData,
};
}

// /////////////////////////////////////
// Create
// /////////////////////////////////////
Expand Down
106 changes: 53 additions & 53 deletions src/collections/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,59 @@ async function update(incomingArgs: Arguments): Promise<Document> {

let { data } = args;

// /////////////////////////////////////
// Upload and resize potential files
// /////////////////////////////////////

if (collectionConfig.upload) {
const fileData: Partial<FileData> = {};

const { staticDir, imageSizes } = collectionConfig.upload;

let staticPath = staticDir;

if (staticDir.indexOf('/') !== 0) {
staticPath = path.join(config.paths.configDir, staticDir);
}

const file = ((req.files && req.files.file) ? req.files.file : req.file) as UploadedFile;

if (file) {
const fsSafeName = await getSafeFilename(staticPath, file.name);

try {
await saveBufferToFile(file.data, `${staticPath}/${fsSafeName}`);

fileData.filename = fsSafeName;
fileData.filesize = file.size;
fileData.mimeType = file.mimetype;

if (isImage(file.mimetype)) {
const dimensions = await getImageSize(`${staticPath}/${fsSafeName}`);
fileData.width = dimensions.width;
fileData.height = dimensions.height;

if (Array.isArray(imageSizes) && file.mimetype !== 'image/svg+xml') {
fileData.sizes = await resizeAndSave(staticPath, collectionConfig, fsSafeName, fileData.mimeType);
}
}
} catch (err) {
throw new FileUploadError();
}

data = {
...data,
...fileData,
};
} else if (data.file === null) {
data = {
...data,
filename: null,
sizes: null,
};
}
}

// /////////////////////////////////////
// beforeValidate - Fields
// /////////////////////////////////////
Expand Down Expand Up @@ -177,59 +230,6 @@ async function update(incomingArgs: Arguments): Promise<Document> {
docWithLocales,
});

// /////////////////////////////////////
// Upload and resize potential files
// /////////////////////////////////////

if (collectionConfig.upload) {
const fileData: Partial<FileData> = {};

const { staticDir, imageSizes } = collectionConfig.upload;

let staticPath = staticDir;

if (staticDir.indexOf('/') !== 0) {
staticPath = path.join(config.paths.configDir, staticDir);
}

const file = ((req.files && req.files.file) ? req.files.file : req.file) as UploadedFile;

if (file) {
const fsSafeName = await getSafeFilename(staticPath, file.name);

try {
await saveBufferToFile(file.data, `${staticPath}/${fsSafeName}`);

fileData.filename = fsSafeName;
fileData.filesize = file.size;
fileData.mimeType = file.mimetype;

if (isImage(file.mimetype)) {
const dimensions = await getImageSize(`${staticPath}/${fsSafeName}`);
fileData.width = dimensions.width;
fileData.height = dimensions.height;

if (Array.isArray(imageSizes) && file.mimetype !== 'image/svg+xml') {
fileData.sizes = await resizeAndSave(staticPath, collectionConfig, fsSafeName, fileData.mimeType);
}
}
} catch (err) {
throw new FileUploadError();
}

result = {
...result,
...fileData,
};
} else if (result.file === null) {
result = {
...result,
filename: null,
sizes: null,
};
}
}

// /////////////////////////////////////
// Handle potential password update
// /////////////////////////////////////
Expand Down
12 changes: 0 additions & 12 deletions src/fields/baseFields/baseUploadFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ export default [
{
name: 'filename',
label: 'Filename',
hooks: {
beforeChange: [
({ req, operation, value }) => {
if (operation === 'create') {
const file = (req.files && req.files.file) ? req.files.file as { name: string } : req.file;
return file.name;
}

return value;
},
],
},
type: 'text',
required: true,
unique: true,
Expand Down
2 changes: 1 addition & 1 deletion src/fields/baseFields/baseVerificationFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Field, FieldHook } from '../config/types';

const autoRemoveVerificationToken: FieldHook = ({ originalDoc, data, value }) => {
// If a user manually sets `_verified` to true,
// and it was `false`, set _verificationToken to `undefined`.
// and it was `false`, set _verificationToken to `null`.
// This is useful because the admin panel
// allows users to set `_verified` to true manually
if (data?._verified === true && originalDoc?._verified === false) {
Expand Down

0 comments on commit 3c42e6e

Please sign in to comment.