Skip to content

Commit

Permalink
Removed use of for (.. in ..).
Browse files Browse the repository at this point in the history
  • Loading branch information
Irwin Razaghi committed Mar 21, 2020
1 parent 7d71dcd commit bf449b7
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions packages/images/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,20 @@ export class ImageService {

let transformedFilename = nodePath.join(this.transformsPath, `${id}${suffix}${format}`);

// If a cached copy of the transformed file exists, don't re-create it
if (await fs.exists(transformedFilename)) {
return res.sendFile(transformedFilename);
}

try {
const sharpImage = sharp(originalPath);
if (resizeOptions) {
await sharpImage.resize(resizeOptions);
if (!(await fs.exists(transformedFilename))) {
try {
const sharpImage = sharp(originalPath);
if (resizeOptions) {
await sharpImage.resize(resizeOptions);
}
await sharpImage.toFile(transformedFilename);
} catch (err) {
console.error(err);
return res.status(500).send({ error: 'Internal server error' });
}
await sharpImage.toFile(transformedFilename);
} catch (err) {
console.error(err);
return res.status(500).send({ error: 'Internal server error' });
}

return res.sendFile(transformedFilename);
});

this.app.get('/image/:id/meta', async (req, res) => {
Expand All @@ -149,18 +148,18 @@ export class ImageService {
});
}
getSrc(id, { format, resize = {} }) {
let url = `${this.protocol}://${this.host}:${this.port}/image/${id}.${format}`;

const url = `${this.protocol}://${this.host}:${this.port}/image/${id}.${format}`;
const searchParams = new URLSearchParams();
for (let key in resize) {
searchParams.set(key, resize[key]);
}

const stringifiedSearchParams = searchParams.toString();
if (stringifiedSearchParams) {
url += `?${stringifiedSearchParams}`;
for (const [param, value] of Object.entries(resize)) {
searchParams.set(param, value);
}
return url;

const queryString = searchParams.toString();

return queryString
? `${url}?${queryString}`
: url;
}
async uploadImage({ stream, originalname }) {
const id = uuid();
Expand Down

0 comments on commit bf449b7

Please sign in to comment.