Merge branch 'master' of github.com:payloadcms/payload into feat/form-onchange
This commit is contained in:
@@ -5,11 +5,11 @@ import { PayloadRequest } from '../../express/types';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { NotFound, Forbidden, ErrorDeletingFile } from '../../errors';
|
||||
import executeAccess from '../../auth/executeAccess';
|
||||
import fileOrDocExists from '../../uploads/fileOrDocExists';
|
||||
import { BeforeOperationHook, Collection } from '../config/types';
|
||||
import { Document, Where } from '../../types';
|
||||
import { hasWhereAccessResult } from '../../auth/types';
|
||||
import { FileData } from '../../uploads/types';
|
||||
import fileExists from '../../uploads/fileExists';
|
||||
|
||||
export type Arguments = {
|
||||
depth?: number
|
||||
@@ -106,7 +106,7 @@ async function deleteQuery(incomingArgs: Arguments): Promise<Document> {
|
||||
|
||||
const fileToDelete = `${staticPath}/${resultToDelete.filename}`;
|
||||
|
||||
if (await fileOrDocExists(Model, staticPath, resultToDelete.filename)) {
|
||||
if (await fileExists(fileToDelete)) {
|
||||
fs.unlink(fileToDelete, (err) => {
|
||||
if (err) {
|
||||
throw new ErrorDeletingFile();
|
||||
@@ -116,8 +116,9 @@ async function deleteQuery(incomingArgs: Arguments): Promise<Document> {
|
||||
|
||||
if (resultToDelete.sizes) {
|
||||
Object.values(resultToDelete.sizes).forEach(async (size: FileData) => {
|
||||
if (await fileOrDocExists(Model, staticPath, size.filename)) {
|
||||
fs.unlink(`${staticPath}/${size.filename}`, (err) => {
|
||||
const sizeToDelete = `${staticPath}/${size.filename}`;
|
||||
if (await fileExists(sizeToDelete)) {
|
||||
fs.unlink(sizeToDelete, (err) => {
|
||||
if (err) {
|
||||
throw new ErrorDeletingFile();
|
||||
}
|
||||
|
||||
@@ -98,28 +98,30 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
|
||||
// Find
|
||||
// /////////////////////////////////////
|
||||
|
||||
let sortParam: Record<string, string>;
|
||||
let sortProperty: string;
|
||||
let sortOrder = 'desc';
|
||||
|
||||
if (!args.sort) {
|
||||
if (collectionConfig.timestamps) {
|
||||
sortParam = { createdAt: 'desc' };
|
||||
sortProperty = 'createdAt';
|
||||
} else {
|
||||
sortParam = { _id: 'desc' };
|
||||
sortProperty = '_id';
|
||||
}
|
||||
} else if (args.sort.indexOf('-') === 0) {
|
||||
sortParam = {
|
||||
[args.sort.substring(1)]: 'desc',
|
||||
};
|
||||
sortProperty = args.sort.substring(1);
|
||||
} else {
|
||||
sortParam = {
|
||||
[args.sort]: 'asc',
|
||||
};
|
||||
sortProperty = args.sort;
|
||||
sortOrder = 'asc';
|
||||
}
|
||||
|
||||
if (sortProperty === 'id') sortProperty = '_id';
|
||||
|
||||
const optionsToExecute = {
|
||||
page: page || 1,
|
||||
limit: limit || 10,
|
||||
sort: sortParam,
|
||||
sort: {
|
||||
[sortProperty]: sortOrder,
|
||||
},
|
||||
lean: true,
|
||||
leanWithId: true,
|
||||
useEstimatedCount,
|
||||
|
||||
10
src/uploads/docWithFilenameExists.ts
Normal file
10
src/uploads/docWithFilenameExists.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { CollectionModel } from '../collections/config/types';
|
||||
|
||||
const docWithFilenameExists = async (Model: CollectionModel, path: string, filename: string): Promise<boolean> => {
|
||||
const doc = await Model.findOne({ filename });
|
||||
if (doc) return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export default docWithFilenameExists;
|
||||
@@ -1,20 +0,0 @@
|
||||
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;
|
||||
@@ -1,6 +1,7 @@
|
||||
import sanitize from 'sanitize-filename';
|
||||
import { CollectionModel } from '../collections/config/types';
|
||||
import fileOrDocExists from './fileOrDocExists';
|
||||
import docWithFilenameExists from './docWithFilenameExists';
|
||||
import fileExists from './fileExists';
|
||||
|
||||
const incrementName = (name: string) => {
|
||||
const extension = name.split('.').pop();
|
||||
@@ -24,7 +25,7 @@ async function getSafeFileName(Model: CollectionModel, staticPath: string, desir
|
||||
let modifiedFilename = desiredFilename;
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
while (await fileOrDocExists(Model, staticPath, modifiedFilename)) {
|
||||
while (await docWithFilenameExists(Model, staticPath, modifiedFilename) || await fileExists(`${staticPath}/${modifiedFilename}`)) {
|
||||
modifiedFilename = incrementName(modifiedFilename);
|
||||
}
|
||||
return modifiedFilename;
|
||||
|
||||
Reference in New Issue
Block a user