chore: merge master
This commit is contained in:
@@ -3,11 +3,14 @@ import { promisify } from 'util';
|
||||
|
||||
const stat = promisify(fs.stat);
|
||||
|
||||
export default async (fileName: string): Promise<boolean> => {
|
||||
const fileExists = async (filename: string): Promise<boolean> => {
|
||||
try {
|
||||
await stat(fileName);
|
||||
await stat(filename);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export default fileExists;
|
||||
|
||||
20
src/uploads/fileOrDocExists.ts
Normal file
20
src/uploads/fileOrDocExists.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import fs from 'fs';
|
||||
import { promisify } from 'util';
|
||||
import { CollectionModel } from '../collections/config/types';
|
||||
|
||||
const stat = promisify(fs.stat);
|
||||
|
||||
const fileOrDocExists = async (Model: CollectionModel, path: string, filename: string): Promise<boolean> => {
|
||||
try {
|
||||
const doc = await Model.findOne({ filename });
|
||||
if (doc) return true;
|
||||
|
||||
await stat(`${path}/${filename}`);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export default fileOrDocExists;
|
||||
@@ -67,6 +67,7 @@ const getBaseUploadFields = ({ config, collection }: Options): Field[] => {
|
||||
label: 'File Name',
|
||||
type: 'text',
|
||||
index: true,
|
||||
unique: true,
|
||||
admin: {
|
||||
readOnly: true,
|
||||
disabled: true,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import sanitize from 'sanitize-filename';
|
||||
import fileExists from './fileExists';
|
||||
import { CollectionModel } from '../collections/config/types';
|
||||
import fileOrDocExists from './fileOrDocExists';
|
||||
|
||||
const incrementName = (name: string) => {
|
||||
const extension = name.split('.').pop();
|
||||
@@ -19,11 +20,11 @@ const incrementName = (name: string) => {
|
||||
return `${incrementedName}.${extension}`;
|
||||
};
|
||||
|
||||
async function getSafeFileName(staticPath: string, desiredFilename: string): Promise<string> {
|
||||
async function getSafeFileName(Model: CollectionModel, staticPath: string, desiredFilename: string): Promise<string> {
|
||||
let modifiedFilename = desiredFilename;
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
while (await fileExists(`${staticPath}/${modifiedFilename}`)) {
|
||||
while (await fileOrDocExists(Model, staticPath, modifiedFilename)) {
|
||||
modifiedFilename = incrementName(modifiedFilename);
|
||||
}
|
||||
return modifiedFilename;
|
||||
|
||||
@@ -7,6 +7,16 @@ import { SanitizedCollectionConfig } from '../collections/config/types';
|
||||
import { FileSizes, ImageSize } from './types';
|
||||
import { PayloadRequest } from '../express/types';
|
||||
|
||||
type Args = {
|
||||
req: PayloadRequest,
|
||||
file: Buffer,
|
||||
dimensions: ProbedImageSize,
|
||||
staticPath: string,
|
||||
config: SanitizedCollectionConfig,
|
||||
savedFilename: string,
|
||||
mimeType: string,
|
||||
}
|
||||
|
||||
function getOutputImage(sourceImage: string, size: ImageSize) {
|
||||
const extension = sourceImage.split('.').pop();
|
||||
const name = sanitize(sourceImage.substr(0, sourceImage.lastIndexOf('.')) || sourceImage);
|
||||
@@ -27,15 +37,15 @@ function getOutputImage(sourceImage: string, size: ImageSize) {
|
||||
* @param mimeType
|
||||
* @returns image sizes keyed to strings
|
||||
*/
|
||||
export default async function resizeAndSave(
|
||||
req: PayloadRequest,
|
||||
file: Buffer,
|
||||
dimensions: ProbedImageSize,
|
||||
staticPath: string,
|
||||
config: SanitizedCollectionConfig,
|
||||
savedFilename: string,
|
||||
mimeType: string,
|
||||
): Promise<FileSizes> {
|
||||
export default async function resizeAndSave({
|
||||
req,
|
||||
file,
|
||||
dimensions,
|
||||
staticPath,
|
||||
config,
|
||||
savedFilename,
|
||||
mimeType,
|
||||
}: Args): Promise<FileSizes> {
|
||||
const { imageSizes, disableLocalStorage } = config.upload;
|
||||
|
||||
const sizes = imageSizes
|
||||
|
||||
Reference in New Issue
Block a user