Skip to content

Commit

Permalink
started buffer addition
Browse files Browse the repository at this point in the history
  • Loading branch information
dills122 committed Aug 1, 2020
1 parent bbc57d6 commit 5566cb8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
24 changes: 24 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Buffer from 'buffer';
import request from 'request';
import { imageHash } from '../src/imageHash';

jest.setTimeout(30000);
Expand Down Expand Up @@ -119,4 +121,26 @@ describe('hash images', () => {
done();
});
});

test('Should handle local file buffer', (done) => {
try {
const testUrl = 'https://www.archives.gov/files/research/american-west/images/west-cover-m.jpgg';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
request({ url: testUrl, encoding: null }, (err, _resp, buffer) => {
if (err) {
return done(err);
}
imageHash(buffer, 16, true, (err, res) => {
if (err) {
return done(err);
}
expect(res).not.toHaveLength(0);
console.log(res);
return done();
});
});
} catch (err) {
return done(err);
}
});
});
38 changes: 34 additions & 4 deletions src/imageHash.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'fs';
import { Buffer } from 'buffer';
import fileType from 'file-type';
import jpeg from 'jpeg-js';
import { PNG } from 'pngjs';
import request from 'request';
import blockhash from './block-hash';
import { URL } from 'url';
import blockhash from './block-hash';

const processPNG = (data, bits, method, cb) => {
try {
Expand All @@ -26,13 +27,35 @@ const processJPG = (data, bits, method, cb) => {
}
};

interface UrlRequestObject {
encoding?: string | null,
url: string | null,
}

interface Base64Object {
encoding?: string | null,
data: string | null
}

// eslint-disable-next-line
export const imageHash = (oldSrc, bits, method, cb) => {
export const imageHash = (oldSrc: string | UrlRequestObject | Base64Object | Buffer, bits, method, cb) => {
const src = oldSrc;

const isBase64Object = (obj: Base64Object | UrlRequestObject): obj is Base64Object => {
const casted = (obj as Base64Object);
return casted.data && casted.encoding && casted.encoding === 'base64';
};

const isUrlRequestObject = (obj: Base64Object | UrlRequestObject): obj is UrlRequestObject => {
const casted = (obj as UrlRequestObject);
return casted.url && casted.url.length > 0;
};

const checkFileType = (name, data) => {
// what is the image type
const type = fileType(data);
console.log(type);
console.log(data);
if (!type || !type.mime) {
cb(new Error('Mime type not found'));
return;
Expand Down Expand Up @@ -87,15 +110,22 @@ export const imageHash = (oldSrc, bits, method, cb) => {
}

// is src url or file
if (typeof src === 'string' && src.indexOf('http') === 0) {
if (typeof src === 'string' && (src.indexOf('http') === 0 || src.indexOf('https') === 0)) {
// url
const req = {
url: src,
encoding: null,
};

request(req, handleRequest);
} else if (typeof src === 'object') {
} else if (Buffer.isBuffer(src)) {
// image buffers
checkFileType('input-img', src);
} else if (typeof src !== 'string' && isBase64Object(src)) {
// Base64 Object
const buffer = new Buffer(src.data, 'base64');
checkFileType('', buffer);
} else if (typeof src !== 'string' && isUrlRequestObject(src)) {
// Request Object
src.encoding = null;
request(src, handleRequest);
Expand Down

0 comments on commit 5566cb8

Please sign in to comment.