ali-flmngr-server-fixed
Version:
> Node.js Backend for Flmngr file manager
162 lines (158 loc) • 8.81 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiskFileSystemRepository = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const crypto_1 = require("crypto");
const util_1 = require("util");
const fs_extra_1 = __importDefault(require("fs-extra"));
const del_1 = __importDefault(require("del"));
const archiver_1 = __importDefault(require("archiver"));
const sharp_1 = __importDefault(require("sharp"));
const utils_1 = require("../utils");
const asyncReaddir = util_1.promisify(fs_1.default.readdir);
const sizeOf = require('image-size');
const path = require("path");
const sharp = require("sharp");
const { encode } = require("blurhash");
class DiskFileSystemRepository {
constructor() {
this.getName = (filePath) => __awaiter(this, void 0, void 0, function* () {
let filename = path.basename(String(filePath));
return (yield filename);
});
this.getFileWidth = (filePath) => __awaiter(this, void 0, void 0, function* () {
const dimensions = sizeOf(filePath);
return (yield dimensions.width);
});
this.getFileHeight = (filePath) => __awaiter(this, void 0, void 0, function* () {
const dimensions = sizeOf(filePath);
return (yield dimensions.height);
});
this.getFileBlurHash = (filePath) => __awaiter(this, void 0, void 0, function* () {
const encodeImageToBlurhash = filePath =>
new Promise((resolve, reject) => {
sharp(filePath)
.raw()
.ensureAlpha()
.resize(32, 32, { fit: "inside" })
.toBuffer((err, buffer, { width, height }) => {
if (err) return reject(err);
resolve(encode(new Uint8ClampedArray(buffer), width, height, 4, 4));
});
});
return (encodeImageToBlurhash(filePath).then(hash => { return (hash); }))
});
this.getDirsInDir = (dirPath) => __awaiter(this, void 0, void 0, function* () {
return (yield asyncReaddir(dirPath))
.map((f) => path_1.default.join(dirPath, f))
.filter((f) => fs_1.default.lstatSync(f).isDirectory());
});
this.getDirectories = (dirPath) => __awaiter(this, void 0, void 0, function* () {
const dirList = yield Promise.all((yield this.getDirsInDir(dirPath)).map(this.getDirectories));
return [dirPath, ...utils_1.flatten(dirList)];
});
this.createDirectory = (dirPath, name) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.mkdir(path_1.default.join(dirPath, name));
});
this.removeDirectory = (dirPath) => __awaiter(this, void 0, void 0, function* () {
yield del_1.default(dirPath, { force: true });
});
this.copyDirectory = (dirPath, newPath) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.copy(dirPath, newPath);
});
this.renameDirectory = (dirPath, newName) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.rename(dirPath, utils_1.getPathWithNewName(dirPath, newName));
});
this.moveDirectory = (dirPath, newPath) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.move(dirPath, newPath);
});
this.downloadDirectory = (dirPath, out) => {
const zip = archiver_1.default('zip');
return new Promise((resolve, reject) => {
zip.directory(dirPath, false).pipe(out);
zip.on('error', reject);
zip.on('end', () => resolve());
zip.finalize();
});
};
this.countFiles = (dirPath) => __awaiter(this, void 0, void 0, function* () {
return (yield asyncReaddir(dirPath))
.map((f) => path_1.default.join(dirPath, f))
.filter((f) => fs_1.default.lstatSync(f).isFile()).length;
});
this.countDirectories = (dirPath) => __awaiter(this, void 0, void 0, function* () {
return (yield asyncReaddir(dirPath))
.filter((f) => fs_1.default.lstatSync(path_1.default.join(dirPath, f)).isDirectory() &&
!['tmp', 'cache'].includes(f)).length;
});
this.getFiles = (dirPath) => __awaiter(this, void 0, void 0, function* () {
return (yield asyncReaddir(dirPath))
.filter((f) => fs_1.default.lstatSync(path_1.default.join(dirPath, f)).isFile())
.map((f) => `${dirPath}/${f}`);
});
this.getFileSize = (filePath) => __awaiter(this, void 0, void 0, function* () { return (yield fs_extra_1.default.stat(filePath)).size; });
this.getFileCreationTime = (filePath) => __awaiter(this, void 0, void 0, function* () { return Math.floor((yield fs_extra_1.default.stat(filePath)).birthtimeMs); });
this.removeFile = (filePath) => __awaiter(this, void 0, void 0, function* () {
yield del_1.default(filePath, { force: true });
});
this.copyFile = (filePath, newPath) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.copy(filePath, newPath);
});
this.renameFile = (filePath, newName) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.rename(filePath, utils_1.getPathWithNewName(filePath, newName));
});
this.moveFile = (filePath, newPath) => __awaiter(this, void 0, void 0, function* () {
yield fs_extra_1.default.move(filePath, newPath);
});
this.isDirectoryOrFileExists = (dirPath) => {
return fs_extra_1.default.pathExistsSync(dirPath);
};
this.getImageOriginal = (filePath) => __awaiter(this, void 0, void 0, function* () { return fs_extra_1.default.readFile(filePath); });
this.getImagePreview = (name, filePath, width, cachePath) => __awaiter(this, void 0, void 0, function* () {
const stats = fs_1.default.statSync(filePath);
const cacheFileName = `${crypto_1.createHash('md5')
.update(filePath + width + stats.size + stats.mtimeMs)
.digest('base64')}.jpg`;
const fullPath = path_1.default.join(cachePath, cacheFileName);
const currentImagePath = path_1.default.join(filePath, name);
try {
return yield fs_extra_1.default.readFile(currentImagePath);
}
catch (error) {
const image = yield sharp_1.default(yield fs_extra_1.default.readFile(filePath))
.resize(500, 500)
.toBuffer();
yield fs_extra_1.default.outputFile(fullPath, image);
return image;
}
});
// this.fileUpload = (filePath) => __awaiter(this, void 0, void 0, function* () {
// const storage = multer.diskStorage({
// destination: function (req, file, cb) {
// cb(null, '/tmp/my-uploads')
// },
// filename: function (req, file, cb) {
// // const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
// cb(null, file.fieldname)
// }
// })
// const upload = multer({ storage: storage })
// return (upload.single('image'))
// });
}
}
exports.DiskFileSystemRepository = DiskFileSystemRepository;
//# sourceMappingURL=DiskFileSystemRepository.js.map