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";
|
2017-06-04 04:35:47 +08:00
|
|
|
import {Logger} from "../Logger";
|
2017-06-11 04:32:56 +08:00
|
|
|
import {diskManagerTask, DiskManagerTask} from "./DiskMangerTask";
|
|
|
|
import {Config} from "../../common/config/private/Config";
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
const Pool = require('threads').Pool;
|
|
|
|
const pool = new Pool();
|
2017-06-04 04:35:47 +08:00
|
|
|
|
|
|
|
const LOG_TAG = "[DiskManager]";
|
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
pool.run(diskManagerTask);
|
2016-05-13 00:24:26 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
export class DiskManager {
|
2017-06-11 04:32:56 +08:00
|
|
|
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);
|
2016-05-13 00:24:26 +08:00
|
|
|
});
|
2017-06-11 04:32:56 +08:00
|
|
|
};
|
|
|
|
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);
|
|
|
|
}
|
2016-05-13 00:24:26 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-01-23 03:30:23 +08:00
|
|
|
|
2017-06-05 00:45:29 +08:00
|
|
|
}
|