diff --git a/node/main-support.ts b/node/main-support.ts index b87d3755..f46d0447 100644 --- a/node/main-support.ts +++ b/node/main-support.ts @@ -4,8 +4,18 @@ * There should be no side-effects of running any of them * They should depend only on their inputs and behave exactly * the same way each time they run no matter the outside state + * + * !!!!!!!!!!!! WARNING !!!!!!!!!!!!!!! + * DANGEROUSLY DEPENDS ON `codeRunningOnMac` when extracting metadata + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ +const codeRunningOnMac: boolean = process.platform === 'darwin'; // <---- MAKES MANY FUNCTIONS NOT PURE !!!! + +import * as path from 'path'; + +const fs = require('fs'); + import type { VhaGlobals } from './main-globals'; import { GLOBALS } from './main-globals'; // TODO -- eliminate dependence on `GLOBALS` in this file! @@ -305,7 +315,7 @@ function getBestStream(metadata) { */ function getFileDuration(metadata): number { if (metadata?.streams?.[0]?.duration) { - + return metadata.streams[0].duration; } else if (metadata?.format?.duration) { @@ -466,7 +476,20 @@ export function extractMetadataAsync( imageElement.width = origWidth; imageElement.fps = realFps; + + // WIP + if (codeRunningOnMac) { + const foundTags: string[] = readTags(filePath); + console.log('tags:', foundTags); + + if (foundTags && foundTags.length) { + imageElement.tags = foundTags; + } + } + + hashFileAsync(filePath, fileStat).then((hash) => { + imageElement.hash = hash; resolve(imageElement); }); @@ -478,6 +501,34 @@ export function extractMetadataAsync( }); } +/** + * If on Mac OS - read the file-system-added file tags and return them + * @param filename + */ +export function readTags(filename): string[] { + + console.log('reading', filename); + + const cmdArr = ['mdls', '-raw', '-name', 'kMDItemUserTags', '"' + filename + '"']; + const cmd = cmdArr.join(' '); + + let foundTags: string[] = undefined; + + exec(cmd, function (error, stdout, stderr) { + if (error) { console.error(error); } + if (stderr) { console.log(stderr); } + if (stdout) { + const tagz: string[] = stdout.toString().split(','); + console.log('Tags in file "' + filename + '": ' + tagz); + + foundTags = tagz; + } + + }); + + return foundTags; +} + /** * Sends progress to Angular App * @param current number