(upload: Upload)
| 49 | } |
| 50 | |
| 51 | public override async create(upload: Upload): Promise<Upload> { |
| 52 | const logger = useLogger(); |
| 53 | const knex = getDatabase(); |
| 54 | |
| 55 | const filesItemsService = new ItemsService<File>('directus_files', { |
| 56 | accountability: this.accountability, |
| 57 | schema: this.schema, |
| 58 | knex, |
| 59 | }); |
| 60 | |
| 61 | upload.creation_date = new Date().toISOString(); |
| 62 | |
| 63 | if (!upload.size || !upload.metadata || !upload.metadata['filename_download']) { |
| 64 | throw ERRORS.INVALID_METADATA; |
| 65 | } |
| 66 | |
| 67 | if (!upload.metadata['type']) { |
| 68 | upload.metadata['type'] = 'application/octet-stream'; |
| 69 | } |
| 70 | |
| 71 | if (!upload.metadata['title']) { |
| 72 | upload.metadata['title'] = formatTitle(upload.metadata['filename_download']); |
| 73 | } |
| 74 | |
| 75 | let existingFile: Record<string, unknown> | undefined; |
| 76 | |
| 77 | // If the payload contains a primary key, we'll check if the file already exists for replacement |
| 78 | if (upload.metadata['id']) { |
| 79 | existingFile = await knex.select('tus_id').from('directus_files').andWhere({ id: upload.metadata['id'] }).first(); |
| 80 | |
| 81 | if (existingFile && existingFile['tus_id'] !== null) { |
| 82 | throw ERRORS.INVALID_METADATA; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const fileData: Partial<File> = { |
| 87 | ...omit(upload.metadata, ['id']), |
| 88 | tus_id: upload.id, |
| 89 | tus_data: upload, |
| 90 | filesize: upload.size, |
| 91 | storage: this.location, |
| 92 | }; |
| 93 | |
| 94 | if (fileData.filename_disk) { |
| 95 | fileData.filename_disk = sanitizeFilepath(fileData.filename_disk); |
| 96 | assertValidStoragePath(fileData.filename_disk, this.location); |
| 97 | await assertUniqueFilename(knex, fileData.filename_disk, upload.metadata['id']); |
| 98 | } |
| 99 | |
| 100 | // If no folder is specified, we'll use the default folder from the settings if it exists |
| 101 | if ('folder' in fileData === false) { |
| 102 | const settings = await knex.select('storage_default_folder').from('directus_settings').first(); |
| 103 | |
| 104 | if (settings?.storage_default_folder) { |
| 105 | fileData.folder = settings.storage_default_folder; |
| 106 | } |
| 107 | } |
| 108 |
no test coverage detected