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

67 lines
1.8 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 {
2017-07-04 01:17:49 +08:00
public static scanDirectory(relativeDirectoryName: string): Promise<DirectoryDTO> {
return new Promise((resolve, reject) => {
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);
2017-07-04 01:17:49 +08:00
let input = <DiskManagerTask.PoolInput>{
relativeDirectoryName,
directoryName,
directoryParent,
absoluteDirectoryName
};
2017-07-04 01:17:49 +08:00
let done = (error: any, result: DirectoryDTO) => {
if (error || !result) {
return reject(error);
}
2017-07-04 01:17:49 +08:00
let addDirs = (dir: DirectoryDTO) => {
dir.photos.forEach((ph) => {
ph.directory = dir;
});
dir.directories.forEach((d) => {
addDirs(d);
});
};
addDirs(result);
return resolve(result);
};
2017-07-04 01:17:49 +08:00
let error = (error) => {
return reject(error);
};
2017-07-04 01:17:49 +08:00
if (Config.Server.enableThreading == true) {
pool.send(input).on('done', done).on('error', error);
} else {
try {
diskManagerTask(input, done);
} catch (err) {
error(err);
}
}
2017-07-04 01:17:49 +08:00
});
}
2017-06-05 00:45:29 +08:00
}