diff --git a/src/bars/files/ImageResizer.ts b/src/bars/files/ImageResizer.ts index 7aba4b6..ecf09bb 100644 --- a/src/bars/files/ImageResizer.ts +++ b/src/bars/files/ImageResizer.ts @@ -1,11 +1,9 @@ import * as fs from "fs"; -import * as os from "os"; -import * as path from "path"; -const sharp = require("sharp"); - -import { IOutputter } from "../../utils/outputter/IOutputter"; import { FileUtils } from "../../utils/FileUtils"; +import { IOutputter } from "../../utils/outputter/IOutputter"; + +const sharp = require("sharp"); const MAX_DIMENSION = 800; @@ -39,7 +37,7 @@ export namespace ImageResizer { outputter: IOutputter, cb: (outPath: string | null, err: any) => void ) { - const outPath = path.join(os.tmpdir(), path.basename(filePath) + ".resized.jpg"); + const outPath = FileUtils.getSmallerFilePath(filePath); // read file to avoid issue where sharp does not release the file lock! fs.readFile(filePath, (err, data) => { diff --git a/src/bars/model/PagingModel.ts b/src/bars/model/PagingModel.ts index eb9c60a..30b8ba6 100644 --- a/src/bars/model/PagingModel.ts +++ b/src/bars/model/PagingModel.ts @@ -1,3 +1,3 @@ export namespace PagingModel { - export const IMAGES_PER_PAGE = 100; + export const IMAGES_PER_PAGE = 80; } diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index b3990f9..3c2e621 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,4 +1,6 @@ import * as fs from "fs"; +import * as os from "os"; +import * as path from "path"; export namespace FileUtils { export function getFilesizeInMegaBytes(filename: string): number { @@ -7,7 +9,25 @@ export namespace FileUtils { return fileSizeInBytes / (1024 * 1024); } + export function getSmallerFilePath(filePath: string): string { + return path.join(os.tmpdir(), path.basename(filePath) + ".resized.jpg"); + } + export function isLargeFile(filePath: string): boolean { return getFilesizeInMegaBytes(filePath) > 0.5; } + + export function isSmallerFileNewer(filePath: string): boolean { + const smallerFilePath = getSmallerFilePath(filePath); + + if (!fs.existsSync(smallerFilePath)) { + return false; + } + + return getModificationDate(smallerFilePath) > getModificationDate(filePath); + } + + function getModificationDate(filePath: string): Date { + return fs.statSync(filePath).mtime; + } } diff --git a/src/utils/ImageResizeExecutor.ts b/src/utils/ImageResizeExecutor.ts index 8edf409..3d48e1a 100644 --- a/src/utils/ImageResizeExecutor.ts +++ b/src/utils/ImageResizeExecutor.ts @@ -29,6 +29,13 @@ export namespace ImageResizeExectutor { }; continue; } + if (FileUtils.isSmallerFileNewer(file)) { + yield { + originalFilepath: file, + smallerFilepath: FileUtils.getSmallerFilePath(file) + }; + continue; + } outputter.infoVerbose(`resizing image at ${file}...`); const stdout = await execShrinkPromise(file);