importedImages.cjs 1.0 KB

1234567891011121314151617181920212223242526272829
  1. const fs = require('node:fs');
  2. const path = require('node:path');
  3. const { getImportedImagesDir } = require('./paths.cjs');
  4. function isPathInsideDirectory(baseDir, targetPath) {
  5. const relative = path.relative(baseDir, targetPath);
  6. return relative === '' || (relative && !relative.startsWith('..') && !path.isAbsolute(relative));
  7. }
  8. function deleteImportedImageBatches(app, scopePrefix) {
  9. const prefix = String(scopePrefix || '').trim();
  10. if (!prefix || !app?.getPath) return;
  11. const baseDir = path.resolve(getImportedImagesDir(app));
  12. if (!fs.existsSync(baseDir)) return;
  13. for (const entry of fs.readdirSync(baseDir, { withFileTypes: true })) {
  14. if (!entry.isDirectory()) continue;
  15. if (entry.name !== prefix && !entry.name.startsWith(`${prefix}-`)) continue;
  16. const targetPath = path.resolve(baseDir, entry.name);
  17. if (!isPathInsideDirectory(baseDir, targetPath) || targetPath === baseDir) continue;
  18. fs.rmSync(targetPath, { recursive: true, force: true });
  19. }
  20. }
  21. module.exports = {
  22. deleteImportedImageBatches,
  23. };