Skip to content
This repository was archived by the owner on Sep 2, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions app/lib/image-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
createFromFile: createFromFile,
createFromUrl: createFromUrl,
createFromData: createFromData,
getFromPost: getFromPost,
putModel: putModel,
}
Expand Down Expand Up @@ -46,6 +47,11 @@ function createFromFile (file, callback) {
}

function createFromUrl (url, callback) {
const scheme = (/^([a-z]+):/i.exec(url)||['']).pop().toLowerCase();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line might be a little too clever, it's hard to tell at a glance what's going on. Should probably break it into steps.

Alternatively, you can probably use url.parse:

const scheme = urlUtil.parse('' + url).scheme
if (scheme === 'data:')
  return createFromData(url, callback);


if (scheme === 'data')
return createFromData(url, callback);

const row = {
slug: hashString(Date.now() + url),
url: url
Expand All @@ -56,6 +62,35 @@ function createFromUrl (url, callback) {
});
}

function createFromData (data, callback) {
if (typeof data === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future reference: I have a library that deals with dataurls called dataurl

const parts = /^(?:data:)?(?:([^;]+);)?(?:charset=(.*);)?(?:([^,]+),)?(.*)$/i.exec(data);

data = {
mimetype: parts[1],
charset: parts[2],
encoding: parts[3],
data: parts[4]
};
}

const mimetype = data.mimetype || 'text/plain';
const charset = data.charset || 'US-ASCII';
const encoding = data.encoding || null;

const buffer = new Buffer(data.data, encoding);

const row = {
slug: hashString(Date.now() + data.data),
mimetype: mimetype,
data: buffer
};

Images.put(row, function (err, result) {
callback(err, result.insertId);
});
}

function putModel(Model) {
return function put(data, image, callback) {
function finish(err, imageId) {
Expand All @@ -79,7 +114,10 @@ function putModel(Model) {

if (image) {
if (typeof image === 'string') {
image = {url: image};
if (/^data:/.test(image))
image = {data: image};
else
image = {url: image};
} else {
image = {
mimetype: image.type,
Expand All @@ -91,7 +129,7 @@ function putModel(Model) {

validationErrors = validationErrors.concat(Images.validateRow(image));

if (!image.size && !image.url) {
if (!image.size && !image.url && !image.data) {
validationErrors.push({
message: "Missing value",
field: 'image'
Expand All @@ -110,5 +148,8 @@ function putModel(Model) {

if (image.url)
return createFromUrl(image.url, finish);

if (image.data)
return createFromData(image.data, finish);
}
}