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

84 lines
3.0 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(scannedDirectory));
2016-04-22 19:23:44 +08:00
});
});
}
2016-05-10 03:43:52 +08:00
private modelToEntity(directroy:any):Directory {
console.log("modelToEntity");
// console.log(directroy);
let directoryEntity = new Directory(directroy._id);
Utils.updateKeys(directoryEntity, directroy);
directroy.photos.forEach((photo) => {
let photoEntity = new Photo(null, null, null, null, null);
Utils.updateKeys(photoEntity, photo);
console.log(photoEntity);
directoryEntity.photos.push(photoEntity);
});
directroy.directories.forEach((dir) => {
let dirEntity = new Directory(null, null, null, null, null, null);
Utils.updateKeys(dirEntity, dir);
console.log(dir);
console.log(dirEntity);
directoryEntity.directories.push(dirEntity);
});
return directoryEntity;
}
2016-04-22 19:23:44 +08:00
}