filmout_manager/dist/files/index.js

216 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Files = void 0;
const promises_1 = require("fs/promises");
const hash_1 = require("../hash");
const path_1 = require("path");
const env_1 = require("../env");
const os_1 = require("os");
const videoExtensions = [
'.mp4',
'.mkv',
'.mov'
];
const imageExtensions = [
'.jpg',
'.jpeg',
'.png',
'.tif',
'.tiff',
'.bmp',
'.dpx'
];
class Files {
2024-10-26 21:11:25 +00:00
static async init(sequencesDir) {
this.sequencesDir = sequencesDir;
return this.exists(sequencesDir);
}
static async exists(path) {
try {
await (0, promises_1.access)(path);
return true;
}
catch {
return false;
}
}
static async enumerateSequenceDirs() {
const sequencesDirs = [
await (0, promises_1.realpath)(this.sequencesDir)
];
let drivesDir = null;
let drives = [];
let drive;
let user;
let stats;
if ((0, os_1.platform)() === 'darwin') {
drivesDir = `/Volumes/`;
}
else if ((0, os_1.platform)() === 'linux') {
user = (0, env_1.envString)('USER', null);
if (user !== null) {
drivesDir = (0, path_1.join)('/media', user);
}
}
if (drivesDir !== null) {
try {
drives = await (0, promises_1.readdir)(drivesDir);
}
catch (err) {
//
}
}
for (let drive of drives) {
if (drive.substring(0, 1) === '.') {
continue;
}
try {
drive = await (0, promises_1.realpath)((0, path_1.join)(drivesDir, drive));
stats = await (0, promises_1.lstat)(drive);
}
catch (err) {
continue;
}
if (stats.isDirectory()) {
sequencesDirs.push(drive);
}
}
return sequencesDirs;
}
2024-10-26 21:11:25 +00:00
static async enumerateSequences() {
const dirs = [];
let all;
let stats;
let dirPath;
const paths = await this.enumerateSequenceDirs();
2024-10-26 21:11:25 +00:00
for (let path of paths) {
try {
all = await (0, promises_1.readdir)(path);
}
catch (err) {
//
2024-10-26 21:11:25 +00:00
}
for (let elem of all) {
if (elem.substring(0, 1) === '.') {
continue;
}
dirPath = (0, path_1.join)(path, elem);
try {
stats = await (0, promises_1.lstat)(dirPath);
}
catch (err) {
continue;
}
if (!stats.isDirectory()) {
continue;
}
if (!await this.isSequence(dirPath)) {
2024-10-26 21:11:25 +00:00
continue;
}
dirs.push({
created: +stats.birthtime,
2024-10-26 21:11:25 +00:00
path: dirPath,
hash: hash_1.Hashes.stringHash(dirPath),
name: (0, path_1.basename)(dirPath)
});
}
}
dirs.sort((a, b) => {
return b.created - a.created;
});
2024-10-26 21:11:25 +00:00
return dirs;
}
static async isSequence(dirPath) {
let sequence = false;
let all;
let stats;
let filePath;
try {
2024-10-26 21:11:25 +00:00
all = await (0, promises_1.readdir)(dirPath);
}
catch (err) {
2024-10-26 21:11:25 +00:00
//
}
for (let elem of all) {
2024-10-26 21:11:25 +00:00
filePath = (0, path_1.join)(dirPath, elem);
try {
2024-10-26 21:11:25 +00:00
stats = await (0, promises_1.lstat)(filePath);
}
catch (err) {
//
}
if (stats.isFile() && elem.substring(0, 1) !== '.' && imageExtensions.indexOf((0, path_1.extname)((0, path_1.basename)(elem).toLowerCase())) !== -1) {
2024-10-26 21:11:25 +00:00
sequence = true;
break;
}
}
2024-10-26 21:11:25 +00:00
return sequence;
}
static async enumerateVideos(path) {
const videos = [];
let all;
let stats;
let filePath;
path = await (0, promises_1.realpath)(path);
try {
all = await (0, promises_1.readdir)(path);
}
catch (err) {
throw err;
}
for (let elem of all) {
filePath = (0, path_1.join)(path, elem);
try {
stats = await (0, promises_1.lstat)(filePath);
if (stats.isFile() && videoExtensions.indexOf((0, path_1.extname)((0, path_1.basename)(elem).toLowerCase())) !== -1) {
videos.push({
created: +stats.birthtime,
path: filePath,
hash: hash_1.Hashes.stringHash(filePath),
name: (0, path_1.basename)(filePath)
});
}
}
catch (err) {
//
}
}
return videos;
}
static async enumerateSequence(path) {
const imgs = [];
let all;
let stats;
let filePath;
path = await (0, promises_1.realpath)(path);
try {
all = await (0, promises_1.readdir)(path);
}
catch (err) {
throw err;
}
for (let elem of all) {
2024-10-26 21:11:25 +00:00
if (elem.substring(0, 1) === '.') {
continue;
}
filePath = (0, path_1.join)(path, elem);
try {
stats = await (0, promises_1.lstat)(filePath);
if (stats.isFile() && imageExtensions.indexOf((0, path_1.extname)((0, path_1.basename)(elem).toLowerCase())) !== -1) {
imgs.push({
path: filePath,
hash: hash_1.Hashes.stringHash(filePath),
name: (0, path_1.basename)(filePath)
});
}
}
catch (err) {
//
}
}
return imgs;
}
}
exports.Files = Files;
module.exports = { Files };
//# sourceMappingURL=index.js.map