Skip to content

Commit

Permalink
feat: Use cached smaller images if available.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrseanryan committed Dec 28, 2018
1 parent fc19028 commit ccf4904
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/bars/files/ImageResizer.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bars/model/PagingModel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export namespace PagingModel {
export const IMAGES_PER_PAGE = 100;
export const IMAGES_PER_PAGE = 80;
}
20 changes: 20 additions & 0 deletions src/utils/FileUtils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/utils/ImageResizeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ccf4904

Please sign in to comment.