1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/DiskManger.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

2016-05-26 03:58:56 +08:00
///<reference path="exif.d.ts"/>
2016-05-09 23:04:56 +08:00
import * as path from "path";
2016-12-28 03:55:51 +08:00
import {DirectoryDTO} from "../../common/entities/DirectoryDTO";
2016-06-25 20:13:06 +08:00
import {ProjectPath} from "../ProjectPath";
import {Logger} from "../Logger";
import {diskManagerTask, DiskManagerTask} from "./DiskMangerTask";
import {Config} from "../../common/config/private/Config";
2016-03-20 02:59:19 +08:00
const Pool = require('threads').Pool;
const pool = new Pool(1);
const LOG_TAG = "[DiskManager]";
pool.run(diskManagerTask);
export class DiskManager {
public static scanDirectory(relativeDirectoryName: string, cb: (error: any, result: DirectoryDTO) => void) {
Logger.silly(LOG_TAG, "scanning directory:", relativeDirectoryName);
let directoryName = path.basename(relativeDirectoryName);
let directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
let absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
let input = <DiskManagerTask.PoolInput>{
relativeDirectoryName,
directoryName,
directoryParent,
absoluteDirectoryName
};
let done = (error: any, result: DirectoryDTO) => {
if (error || !result) {
return cb(error, result);
}
let addDirs = (dir: DirectoryDTO) => {
dir.photos.forEach((ph) => {
ph.directory = dir;
});
dir.directories.forEach((d) => {
addDirs(d);
});
};
addDirs(result);
return cb(error, result);
};
let error = (error) => {
return cb(error, null);
};
if (Config.Server.enableThreading == true) {
pool.send(input).on('done', done).on('error', error);
} else {
try {
diskManagerTask(input, done);
} catch (err) {
error(err);
}
}
}
2017-06-05 00:45:29 +08:00
}