-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
52 lines (52 loc) · 2.26 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 });
const fs_1 = __importDefault(require("fs"));
const Jimp = require("jimp");
// filterImageFromURL
// helper function to download, filter, and save the filtered image locally
// returns the absolute path to the local image
// INPUTS
// inputURL: string - a publicly accessible url to an image file
// RETURNS
// an absolute path to a filtered image locally saved file
function filterImageFromURL(inputURL) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
const photo = yield Jimp.read(inputURL);
const outpath = '/tmp/filtered.' + Math.floor(Math.random() * 2000) + '.jpg';
yield photo
.resize(256, 256) // resize
.quality(60) // set JPEG quality
.greyscale() // set greyscale
.write(__dirname + outpath, (img) => {
resolve(__dirname + outpath);
});
}));
});
}
exports.filterImageFromURL = filterImageFromURL;
// deleteLocalFiles
// helper function to delete files on the local disk
// useful to cleanup after tasks
// INPUTS
// files: Array<string> an array of absolute paths to files
function deleteLocalFiles(files) {
return __awaiter(this, void 0, void 0, function* () {
for (let file of files) {
fs_1.default.unlinkSync(file);
}
});
}
exports.deleteLocalFiles = deleteLocalFiles;
//# sourceMappingURL=util.js.map