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
2016-05-04 19:05:19 +02:00

56 lines
1.8 KiB
TypeScript

import * as path from 'path';
import {Directory} from "../../../common/entities/Directory";
import {IGalleryManager} from "../IGalleryManager";
import {DiskManager} from "../DiskManger";
import {Utils} from "../../../common/Utils";
import {DirectoryModel} from "./entities/DirectoryModel";
import {PhotoModel} from "./entities/PhotoModel";
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),"/");
DirectoryModel.findOne({name:directoryName, path: directoryParent}).populate('photos').populate('directories').exec( (err,res:any) =>{
if(err || !res){
return this.indexDirectory(relativeDirectoryName,cb);
}
return cb(err, res);
});
}
public indexDirectory(relativeDirectoryName, cb:(error: any,result:Directory) => void){
DiskManager.scanDirectory(relativeDirectoryName,(err,scannedDirectory)=>{
let arr = [];
scannedDirectory.directories.forEach((value) => {
let dir = new DirectoryModel(value);
Utils.setKeys(dir,value);
dir.save();
arr.push(dir);
});
scannedDirectory.directories = arr;
arr = [];
scannedDirectory.photos.forEach((value) => {
let p = new PhotoModel(value);
Utils.setKeys(p,value);
p.save();
arr.push(p);
});
scannedDirectory.photos = arr;
DirectoryModel.create(scannedDirectory,(err)=>{
return cb(err,scannedDirectory);
});
});
}
}