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

79 lines
2.8 KiB
TypeScript
Raw Normal View History

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-05-10 03:43:52 +08:00
import {Photo} from "../../../common/entities/Photo";
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-10 03:43:52 +08:00
return cb(err, this.modelToEntity(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-10 03:43:52 +08:00
DirectoryModel.create(scannedDirectory, (err, savedDir)=> {
scannedDirectory.photos.forEach((value:any) => {
value['directory'] = savedDir;
value.save();
});
return cb(err, this.modelToEntity(savedDir));
2016-04-22 19:23:44 +08:00
});
});
}
private modelToEntity(directoryModel:any):Directory {
let directoryEntity = new Directory(directoryModel._id, directoryModel.name, directoryModel.path, directoryModel.lastupdate, [], []);
directoryModel.photos.forEach((photo) => {
let photoEntity = new Photo(photo._id, photo.name, null, photo.width, photo.height);
2016-05-10 03:43:52 +08:00
directoryEntity.photos.push(photoEntity);
});
directoryModel.directories.forEach((dir) => {
let dirEntity = new Directory(dir._id, dir.name, dir.path, dir.lastupdate, [], []);
2016-05-10 03:43:52 +08:00
directoryEntity.directories.push(dirEntity);
});
return directoryEntity;
}
2016-04-22 19:23:44 +08:00
}