2016-05-09 23:04:56 +08:00
|
|
|
import * as path from "path";
|
2016-04-22 19:23:44 +08:00
|
|
|
import {Directory} from "../../../common/entities/Directory";
|
|
|
|
import {IGalleryManager} from "../IGalleryManager";
|
|
|
|
import {DiskManager} from "../DiskManger";
|
|
|
|
import {Utils} from "../../../common/Utils";
|
2016-05-05 00:34:54 +08:00
|
|
|
import {DirectoryModel} from "./entities/DirectoryModel";
|
|
|
|
import {PhotoModel} from "./entities/PhotoModel";
|
2016-04-22 19:23:44 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class MongoGalleryManager implements IGalleryManager {
|
2016-04-22 19:23:44 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
constructor() {
|
2016-04-22 19:23:44 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public listDirectory(relativeDirectoryName, cb:(error:any, result:Directory) => void) {
|
2016-04-22 19:23:44 +08:00
|
|
|
let directoryName = path.basename(relativeDirectoryName);
|
2016-05-09 23:04:56 +08:00
|
|
|
let directoryParent = path.join(path.dirname(relativeDirectoryName), "/");
|
|
|
|
|
|
|
|
DirectoryModel.findOne({
|
|
|
|
name: directoryName,
|
|
|
|
path: directoryParent
|
|
|
|
}).populate('photos').populate('directories').exec((err, res:any) => {
|
|
|
|
if (err || !res) {
|
|
|
|
return this.indexDirectory(relativeDirectoryName, cb);
|
2016-04-22 19:23:44 +08:00
|
|
|
}
|
2016-05-05 00:34:54 +08:00
|
|
|
return cb(err, res);
|
2016-04-22 19:23:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public indexDirectory(relativeDirectoryName, cb:(error:any, result:Directory) => void) {
|
|
|
|
DiskManager.scanDirectory(relativeDirectoryName, (err, scannedDirectory)=> {
|
2016-04-22 19:23:44 +08:00
|
|
|
let arr = [];
|
|
|
|
scannedDirectory.directories.forEach((value) => {
|
2016-05-05 00:34:54 +08:00
|
|
|
let dir = new DirectoryModel(value);
|
2016-05-09 23:04:56 +08:00
|
|
|
Utils.setKeys(dir, value);
|
2016-04-22 19:23:44 +08:00
|
|
|
dir.save();
|
|
|
|
arr.push(dir);
|
|
|
|
});
|
|
|
|
scannedDirectory.directories = arr;
|
|
|
|
arr = [];
|
|
|
|
scannedDirectory.photos.forEach((value) => {
|
2016-05-05 00:34:54 +08:00
|
|
|
let p = new PhotoModel(value);
|
2016-05-09 23:04:56 +08:00
|
|
|
Utils.setKeys(p, value);
|
2016-04-22 19:23:44 +08:00
|
|
|
p.save();
|
|
|
|
arr.push(p);
|
|
|
|
});
|
|
|
|
|
|
|
|
scannedDirectory.photos = arr;
|
2016-05-09 23:04:56 +08:00
|
|
|
DirectoryModel.create(scannedDirectory, (err)=> {
|
|
|
|
return cb(err, scannedDirectory);
|
2016-04-22 19:23:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|