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

63 lines
2.0 KiB
TypeScript
Raw Normal View History

2016-05-09 23:04:56 +08:00
import * as fs from "fs";
import * as path from "path";
import * as mime from "mime";
import * as sizeOf from "image-size";
2016-03-20 17:49:49 +08:00
import {Directory} from "../../common/entities/Directory";
import {Photo} from "../../common/entities/Photo";
2016-03-20 02:59:19 +08:00
2016-05-09 23:04:56 +08:00
export class DiskManager {
public static scanDirectory(relativeDirectoryName, cb:(error:any, result:Directory) => void) {
2016-04-22 19:23:44 +08:00
console.log("DiskManager: scanDirectory");
2016-03-20 17:49:49 +08:00
let directoryName = path.basename(relativeDirectoryName);
2016-05-09 23:04:56 +08:00
let directoryParent = path.join(path.dirname(relativeDirectoryName), "/");
let absoluteDirectoryName = path.join(__dirname, "/../../demo/images", relativeDirectoryName);
2016-03-20 02:59:19 +08:00
2016-05-09 23:04:56 +08:00
let directory = new Directory(1, directoryName, directoryParent, new Date(), [], []);
2016-03-20 17:49:49 +08:00
fs.readdir(absoluteDirectoryName, function (err, list) {
2016-05-09 23:04:56 +08:00
if (err) {
return cb(err, null);
2016-03-20 17:49:49 +08:00
}
for (let i = 0; i < list.length; i++) {
let file = list[i];
let fullFilePath = path.resolve(absoluteDirectoryName, file);
2016-05-09 23:04:56 +08:00
if (fs.statSync(fullFilePath).isDirectory()) {
directory.directories.push(new Directory(2, file, relativeDirectoryName, new Date(), [], []));
2016-03-20 17:49:49 +08:00
}
2016-05-09 23:04:56 +08:00
if (DiskManager.isImage(fullFilePath)) {
let dimensions = sizeOf(fullFilePath);
2016-05-09 23:04:56 +08:00
directory.photos.push(new Photo(1, file, dimensions.width, dimensions.height));
2016-03-20 17:49:49 +08:00
}
}
return cb(err, directory);
});
2016-03-20 02:59:19 +08:00
}
2016-03-20 17:49:49 +08:00
2016-05-09 23:04:56 +08:00
private static isImage(fullPath) {
2016-03-20 17:49:49 +08:00
let imageMimeTypes = [
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/tiff',
'image/webp',
'image/x-tiff',
'image/x-windows-bmp'
];
var extension = mime.lookup(fullPath);
if (imageMimeTypes.indexOf(extension) !== -1) {
return true;
}
return false;
2016-03-20 02:59:19 +08:00
}
}