Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from BBVAEngineering/persistent-filter
Browse files Browse the repository at this point in the history
Persistent filter
  • Loading branch information
josex2r authored Feb 14, 2019
2 parents 060ba21 + 8cd9f10 commit 1f4c01c
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 215 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ module.exports = {
// ...

treeForPublic() {
return new Thumbnail('assets/images');
return new Thumbnail('assets/images', { /* options */ });
}
};
```

## Options

| Option | Type | Defaults | Description |
|--------|----------|-------------------------------|-------------------------------------------|
| prefix | `String` | `thumbnail_` | Prefix to be added on each thumbnail name |
| globs | `Array` | `['**/*.(jpg|jpeg|gif|png)']` | Files to be processed |
| width | `Number` | `64` | Thumbnail width |
| Option | Type | Defaults | Description |
|------------|-----------|---------------------------------|-------------------------------------------|
| prefix | `String` | `thumbnail_` | Prefix to be added on each thumbnail name |
| extensions | `Array` | `['jpg', 'jpeg', 'gif', 'png']` | Files to be processed |
| persist | `Boolean` | `false` | Use disk cache |
| width | `Number` | `64` | Thumbnail width |

## Versioning

Expand Down
75 changes: 39 additions & 36 deletions lib/thumbnail.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
'use strict';

const Plugin = require('broccoli-caching-writer');
const Filter = require('broccoli-persistent-filter');
const md5Hex = require('broccoli-persistent-filter/lib/md5-hex');
const path = require('path');
const fs = require('fs-extra');
const walkSync = require('walk-sync');
const sharp = require('sharp');

async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
class Thumbnail extends Filter {
constructor(inputNode, options = {}) {
super(inputNode, {
persist: options.persist,
annotation: 'Thumbnail',
'async': true // eslint-disable-line
});

class Thumbnail extends Plugin {
constructor(inputNode, options) {
options = Object.assign({
this.options = Object.assign({
prefix: 'thumbnail_',
globs: [
'**/*.{jpg,jpeg,gif,png}'
],
extensions: ['jpg', 'jpeg', 'gif', 'png'],
width: 64
}, options);
}

super([inputNode], options);
this.options = options;
baseDir() {
return path.join(__dirname, '..');
}

async build() {
const { globs, prefix, width } = this.options;
const inputPath = this.inputPaths[0];
const allInputFiles = walkSync(inputPath, { directories: false });
const images = walkSync(inputPath, { directories: false, globs });
getDestFilePath(relativePath) {
const { extensions, prefix } = this.options;
const ext = path.extname(relativePath).replace(/^\./, '');

await asyncForEach(images, async(relativePath) => {
const absolutePath = path.join(inputPath, relativePath);
if (extensions.includes(ext)) {
const basename = path.basename(relativePath);
const dirname = path.dirname(relativePath);
const thumbnailRelativePath = path.join(dirname, `${prefix}${basename}`);
const image = sharp(absolutePath);

await fs.ensureDir(path.join(this.outputPath, dirname));
return thumbnailRelativePath;
}

return null;
}

const metadata = await image.metadata();
const height = parseInt(metadata.height * width / metadata.width, 10);
cacheKeyProcessString(string, relativePath) {
return md5Hex(string.length + 0x00 + relativePath);
}

await image
.resize(width, height)
.toFile(path.join(this.outputPath, thumbnailRelativePath));
});
async processString(content, relativePath) {
const { width } = this.options;
const absolutePath = path.resolve(this.inputPaths[0], relativePath);
const image = sharp(absolutePath);
const metadata = await image.metadata();
const height = parseInt(metadata.height * width / metadata.width, 10);
const output = await image
.resize(width, height)
.toBuffer();

await asyncForEach(allInputFiles, async(relativePath) => {
const absolutePath = path.join(inputPath, relativePath);
const outputPath = path.join(this.outputPath, relativePath);
return { output };
}

await fs.ensureLink(absolutePath, outputPath);
});
postProcess(results) {
return { output: results.output };
}
}

Expand Down
Loading

0 comments on commit 1f4c01c

Please sign in to comment.