-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cf-images): creating cloudflare images adapter (#7)
#6 * feat: creating cloudflare adapter * feat: cloudflare integration * feat: cloudflare integration * feat: cloudflare integration * feat: cloudflare integration * fix: ecom review #1 * fix: ecom review #1 * fix: ecom review #3 * fix: ecom review #4 * fix: ecom review #4 * fix: ecom review #5 * fix: ecom review #6 * fix: ecom review #7 * fix: ecom review #8
- Loading branch information
1 parent
534c406
commit 6fe1b0d
Showing
8 changed files
with
1,043 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,4 @@ typings/ | |
|
||
# dotenv environment variables file | ||
.env | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
|
||
// Dependencies | ||
const axios = require('axios').default | ||
const FormData = require('form-data') | ||
const download = require('./RequestDownload') | ||
|
||
// Cloudflare module | ||
module.exports = (auth) => { | ||
|
||
// Setup axios client with api key | ||
const cloudflareClient = axios.create({ | ||
baseURL: `https://api.cloudflare.com/client/v4/accounts/${auth.accountId}`, | ||
headers: { Authorization: `Bearer ${auth.apiKey}` } | ||
}) | ||
|
||
// Function to Compress image | ||
return function (url, size = 'normal', __callback = () => {}) { | ||
|
||
// API Payload | ||
let options = new FormData() | ||
options.append('file', url) | ||
|
||
// Force timeout with 20s | ||
let callbackSent = false | ||
const callback = (err, data) => { | ||
if (!callbackSent) { | ||
callbackSent = true | ||
__callback(err, data) | ||
} | ||
if (timer) { | ||
clearTimeout(timer) | ||
} | ||
} | ||
|
||
// Verify if responded in 20s | ||
const timer = setTimeout(() => { | ||
callback(new Error('Cloudflare optimization timed out')) | ||
logger.log(`Cloudflare timed out`) | ||
}, 20000) | ||
|
||
// Upload image to cloudflare | ||
cloudflareClient({ | ||
method: 'POST', | ||
url: '/images/v1', | ||
headers: { ...options.getHeaders() }, | ||
data: options | ||
}).then((response) => { | ||
const id = response.data.result.id | ||
const variants = response.data.result.variants | ||
const url = variants.find(v => v.endsWith(`/${size}`)) || variants[0] | ||
if (url) { | ||
download(url, { 'Accept': 'image/webp,image/*,*/*;q=0.8' }, (err, imageBody) => { | ||
if (err) logger.error(err) | ||
callback(err, { | ||
...response.data.result, | ||
url: normalImage, | ||
imageBody | ||
}) | ||
setTimeout(() => cloudflareClient.delete(`/images/v1/${id}`), 60000) | ||
}) | ||
} | ||
}).catch((err) => callback(err)) | ||
} | ||
} |
Oops, something went wrong.