Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from BaiYinSilver/master
Browse files Browse the repository at this point in the history
Update image resize function to use consistent coding style
  • Loading branch information
mhopkins-msft committed Jul 2, 2020
2 parents 53a4202 + d24607f commit ff8f928
Show file tree
Hide file tree
Showing 4 changed files with 452 additions and 442 deletions.
43 changes: 18 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This sample implements a function triggered by Azure Blob Storage to resize an i

The key aspects of this sample are in the function bindings and implementation.

This sample is used by the topic [Tutorial: Automate resizing uploaded images using Event Grid](https://docs.microsoft.com/en-us/azure/event-grid/resize-images-on-storage-blob-upload-event?tabs=nodejsv10#deploy-the-function-code/)

## Function bindings

In order to interface with image data, you need to configure the function to process binary data.
Expand Down Expand Up @@ -86,38 +88,29 @@ module.exports = (context, eventGridEvent, inputBlob) => {
const blobUrl = context.bindingData.data.url;
const blobName = blobUrl.slice(blobUrl.lastIndexOf("/")+1);

Jimp.read(inputBlob).then( (thumbnail) => {

thumbnail.resize(widthInPixels, Jimp.AUTO);

const options = {
contentSettings: { contentType: contentType }
};

thumbnail.getBuffer(Jimp.MIME_PNG, async (err, buffer) => {

const readStream = stream.PassThrough();
readStream.end(buffer);

const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
const image = await Jimp.read(inputBlob);
const thumbnail = image.resize(widthInPixels, Jimp.AUTO);
const thumbnailBuffer = await thumbnail.getBufferAsync(Jimp.AUTO);
const readStream = stream.PassThrough();
readStream.end(thumbnailBuffer);

try {
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
try {

await uploadStreamToBlockBlob(aborter, readStream,
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers);
await uploadStreamToBlockBlob(aborter, readStream,
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers,
{ blobHTTPHeaders: { blobContentType: "image/*" } });

} catch (err) {
} catch (err) {

context.log(err.message);
context.log(err.message);

} finally {
} finally {

context.done();
context.done();

}
});
});
}
};
```

Expand Down
44 changes: 17 additions & 27 deletions Thumbnail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,27 @@ module.exports = (context, eventGridEvent, inputBlob) => {
const blobUrl = context.bindingData.data.url;
const blobName = blobUrl.slice(blobUrl.lastIndexOf("/")+1);

Jimp.read(inputBlob).then( (thumbnail) => {
const image = await Jimp.read(inputBlob);
const thumbnail = image.resize(widthInPixels, Jimp.AUTO);
const thumbnailBuffer = await thumbnail.getBufferAsync(Jimp.AUTO);
const readStream = stream.PassThrough();
readStream.end(thumbnailBuffer);

thumbnail.resize(widthInPixels, Jimp.AUTO);
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
try {

const options = {
contentSettings: { contentType: contentType }
};
await uploadStreamToBlockBlob(aborter, readStream,
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers,
{ blobHTTPHeaders: { blobContentType: "image/*" } });

thumbnail.getBuffer(Jimp.MIME_PNG, async (err, buffer) => {
} catch (err) {

const readStream = stream.PassThrough();
readStream.end(buffer);
context.log(err.message);

const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
} finally {

try {
context.done();

await uploadStreamToBlockBlob(aborter, readStream,
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers,
{ blobHTTPHeaders: { blobContentType: "image/jpeg" } });

} catch (err) {

context.log(err.message);

} finally {

context.done();

}
});
});
};
}
};
Loading

0 comments on commit ff8f928

Please sign in to comment.