From d1e6843f3eefcd3103760ecf491b63273dd8f491 Mon Sep 17 00:00:00 2001 From: Kokul Shanmugharajah Date: Fri, 22 Mar 2024 06:00:27 -0700 Subject: [PATCH] feat(cli) CLI when uploading photo.EXT, it detects both photo.EXT.xmp and photo.xmp (#8186) * Initial implementation * chore: remove duplicate access check --------- Co-authored-by: Jason Rasmussen --- cli/src/commands/asset.ts | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/cli/src/commands/asset.ts b/cli/src/commands/asset.ts index b6c159c9ba..e7a924aaf2 100644 --- a/cli/src/commands/asset.ts +++ b/cli/src/commands/asset.ts @@ -14,7 +14,7 @@ import { createHash } from 'node:crypto'; import fs, { createReadStream } from 'node:fs'; import { access, constants, stat, unlink } from 'node:fs/promises'; import os from 'node:os'; -import { basename } from 'node:path'; +import path, { basename } from 'node:path'; import { CrawlService } from 'src/services/crawl.service'; import { BaseOptions, authenticate } from 'src/utils'; @@ -61,13 +61,34 @@ class Asset { throw new Error('File modified at not set'); } - // TODO: doesn't xmp replace the file extension? Will need investigation - const sideCarPath = `${this.path}.xmp`; + // XMP sidecars can come in two filename formats. For a photo named photo.ext, the filenames are photo.ext.xmp and photo.xmp + const assetPath = path.parse(this.path); + const assetPathWithoutExt = path.join(assetPath.dir, assetPath.name); + const sidecarPathWithoutExt = `${assetPathWithoutExt}.xmp`; + const sideCarPathWithExt = `${this.path}.xmp`; + + const [sideCarWithExtExists, sideCarWithoutExtExists] = await Promise.all([ + access(sideCarPathWithExt, constants.R_OK) + .then(() => true) + .catch(() => false), + access(sidecarPathWithoutExt, constants.R_OK) + .then(() => true) + .catch(() => false), + ]); + + let sidecarPath = undefined; + if (sideCarWithExtExists) { + sidecarPath = sideCarPathWithExt; + } else if (sideCarWithoutExtExists) { + sidecarPath = sidecarPathWithoutExt; + } + let sidecarData: Blob | undefined = undefined; - try { - await access(sideCarPath, constants.R_OK); - sidecarData = new File([await fs.openAsBlob(sideCarPath)], basename(sideCarPath)); - } catch {} + if (sidecarPath) { + try { + sidecarData = new File([await fs.openAsBlob(sidecarPath)], basename(sidecarPath)); + } catch {} + } const data: any = { assetData: new File([await fs.openAsBlob(this.path)], basename(this.path)),