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

Commit ff8f928

Browse files
Merge pull request #2 from BaiYinSilver/master
Update image resize function to use consistent coding style
2 parents 53a4202 + d24607f commit ff8f928

File tree

4 files changed

+452
-442
lines changed

4 files changed

+452
-442
lines changed

README.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This sample implements a function triggered by Azure Blob Storage to resize an i
1515

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

18+
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/)
19+
1820
## Function bindings
1921

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

89-
Jimp.read(inputBlob).then( (thumbnail) => {
90-
91-
thumbnail.resize(widthInPixels, Jimp.AUTO);
92-
93-
const options = {
94-
contentSettings: { contentType: contentType }
95-
};
96-
97-
thumbnail.getBuffer(Jimp.MIME_PNG, async (err, buffer) => {
98-
99-
const readStream = stream.PassThrough();
100-
readStream.end(buffer);
101-
102-
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
103-
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
91+
const image = await Jimp.read(inputBlob);
92+
const thumbnail = image.resize(widthInPixels, Jimp.AUTO);
93+
const thumbnailBuffer = await thumbnail.getBufferAsync(Jimp.AUTO);
94+
const readStream = stream.PassThrough();
95+
readStream.end(thumbnailBuffer);
10496

105-
try {
97+
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
98+
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
99+
try {
106100

107-
await uploadStreamToBlockBlob(aborter, readStream,
108-
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers);
101+
await uploadStreamToBlockBlob(aborter, readStream,
102+
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers,
103+
{ blobHTTPHeaders: { blobContentType: "image/*" } });
109104

110-
} catch (err) {
105+
} catch (err) {
111106

112-
context.log(err.message);
107+
context.log(err.message);
113108

114-
} finally {
109+
} finally {
115110

116-
context.done();
111+
context.done();
117112

118-
}
119-
});
120-
});
113+
}
121114
};
122115
```
123116

Thumbnail/index.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,27 @@ module.exports = (context, eventGridEvent, inputBlob) => {
3737
const blobUrl = context.bindingData.data.url;
3838
const blobName = blobUrl.slice(blobUrl.lastIndexOf("/")+1);
3939

40-
Jimp.read(inputBlob).then( (thumbnail) => {
40+
const image = await Jimp.read(inputBlob);
41+
const thumbnail = image.resize(widthInPixels, Jimp.AUTO);
42+
const thumbnailBuffer = await thumbnail.getBufferAsync(Jimp.AUTO);
43+
const readStream = stream.PassThrough();
44+
readStream.end(thumbnailBuffer);
4145

42-
thumbnail.resize(widthInPixels, Jimp.AUTO);
46+
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
47+
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
48+
try {
4349

44-
const options = {
45-
contentSettings: { contentType: contentType }
46-
};
50+
await uploadStreamToBlockBlob(aborter, readStream,
51+
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers,
52+
{ blobHTTPHeaders: { blobContentType: "image/*" } });
4753

48-
thumbnail.getBuffer(Jimp.MIME_PNG, async (err, buffer) => {
54+
} catch (err) {
4955

50-
const readStream = stream.PassThrough();
51-
readStream.end(buffer);
56+
context.log(err.message);
5257

53-
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
54-
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
58+
} finally {
5559

56-
try {
60+
context.done();
5761

58-
await uploadStreamToBlockBlob(aborter, readStream,
59-
blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers,
60-
{ blobHTTPHeaders: { blobContentType: "image/jpeg" } });
61-
62-
} catch (err) {
63-
64-
context.log(err.message);
65-
66-
} finally {
67-
68-
context.done();
69-
70-
}
71-
});
72-
});
73-
};
62+
}
63+
};

0 commit comments

Comments
 (0)