Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'image-downloader' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
let retryDownloads = () => {
if (failedDownloads.length > 0 && !alreadySavedData) {
const fail = failedDownloads[0]
if (fail === lastFail) {
failCount++
} else {
failCount = 0
}
// Stop trying after 10 fails
if (failCount < 10) {
console.log(`failed downloads left: ${failedDownloads.length}`)
download
.image({
url: fail.url,
dest: fail.dest,
timeout: 15000
})
.then(() => {
// Delete element from fails list
failedDownloads.splice(0, 1)
console.log('FIXED failed download of ' + fail.url)
if (failedDownloads.length > 0) {
if (!downloadsAreDone) {
retryDownloads()
}
} else {
// Finished downloads
console.log('downloads are done')
async url() {
const { ctx, service } = this
// 组装参数
const attachment = new this.ctx.model.Attachment
const { url } = ctx.request.body
const filename = path.basename(url) // 文件名称
const extname = path.extname(url).toLowerCase() // 文件扩展名称
const options = {
url: url,
dest: path.join(this.config.baseDir, 'app/public/uploads', `${attachment._id.toString()}${extname}`)
}
let res
try {
// 写入文件 const { filename, image}
await download.image(options)
attachment.extname = extname
attachment.filename = filename
attachment.url = `/uploads/${attachment._id.toString()}${extname}`
res = await service.upload.create(attachment)
} catch (err) {
throw err
}
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
}
// Image: Generate from Data URL
if (imageProtocol === 'data:') {
resizeWriteImage(dataUriToBuffer(imageUrl), imageFilepathTemporary, notificationIconWidth, (error, imageFilepathConverted) => {
if (error) { return }
notificationOptions.icon = imageFilepathConverted
showNotification(notificationOptions, decoratedPush)
})
return
}
// Image: Download from Web
imageDownloader.image({ url: imageUrl, dest: imageFilepathTemporary })
.then((result) => {
const imageFilepathDownloaded = result.filename
const imageBuffer = result.image
const imageType = fileType(imageBuffer)
const isIco = icojs.isICO(imageBuffer)
const isPng = imageType.mime === 'image/png'
const isJpeg = imageType.mime === 'image/jpg' || imageType.mime === 'image/jpeg'
// From .PNG
if (isPng || isJpeg) {
resizeWriteImage(imageBuffer, imageFilepathDownloaded, notificationIconWidth, (error, imageFilepathConverted) => {
if (error) { return }
notificationOptions.icon = imageFilepathConverted
showNotification(notificationOptions, decoratedPush)
})
return __awaiter(this, void 0, void 0, function* () {
const options = {
url: url,
dest: `${__dirname}/../cache/temp.${url.split('.').pop()}`,
};
const { filename } = yield imageDownloader.image(options);
return filename;
});
}
function downloadWall(url, localPath, search, name, username, fname, lname, html) {
console.log('Downloading Wallpaper From => ', url, ' To => ', localPath);
options = {
url: url,
dest: localPath,
timeout: 5000
}
download = downloader.image(options).then(({ filename, image }) => {
if (downloads[search] == 1) {
delete downloads[search];
} else {
downloads[search] = downloads[search] - 1;
}
store.set('downloads', downloads);
tags[search] = tags[search] + 1;
store.set('tags', tags);
walls[name + '.jpg'] = [search, username, fname, lname, html];
store.set('walls', walls);
console.log('Updated pending download = ', downloads);
return Promise.all(remoteAssets.map(a => download.image(a)))
.then(() => coreutils.logger.ok(`Downloaded ${remoteAssets.length} remote assets`))
async saveImage(imageUrls: [string], dest = "/tmp/images"): Promise {
try {
for (let imageUrl of imageUrls) {
const { filename, image } = await download.image({
url: imageUrl,
dest
});
}
return true;
} catch (e) {
errorLogger.error(e.message);
return false;
}
}
}