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

56 lines
1.8 KiB
TypeScript
Raw Normal View History

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