-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise3.js
More file actions
61 lines (52 loc) · 1.99 KB
/
exercise3.js
File metadata and controls
61 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 1. Install Node fetch if ypur node version is less than 18
* 2. Import "fs" from "fs/promises" which is built-in to NodeJS
* 3. Go to "wallhaven.cc" and find a wallpaper and "open image in new tab" to have only the image in the tab
* The URL should end with .png or .jpg
* Eg.: https://w.wallhaven.cc/full/7p/wallhaven-7p39gy.png
* 4. Use fetch to get this image and fetch it correctly, then use "fs" to write a file to your filesystem with the actual image
* itself so you can open it after you run the script. Note that teses functions in fs return promises
* 5. This is specially challenging to make you google and see how can you save the image correctly.
*/
import {writeFile} from 'fs'
async function fetchImage() {
try {
const response = await fetch('https://w.wallhaven.cc/full/7p/wallhaven-7p39gy.png')
if(!response.ok) {
throw new Error(`Reponse failed ${response.status}`);
}
const data = await response.arrayBuffer()
const buffer = Buffer.from(data)
console.log("🚀 ~ fetchImage ~ buffer:", buffer)
writeFile('image1.png', buffer, (err) => {
if (err) console.error(err.message)
console.log("Image was saved succesfully")
})
} catch (error) {
console.error(error.message)
}
}
fetchImage()
async function fetchImageBlob() {
try {
const response = await fetch('https://w.wallhaven.cc/full/7p/wallhaven-7p39gy.png')
if (!response.ok) {
throw new Error(`Request failes status code is ${response.status}`)
}
const data = await response.blob()
data.name = 'image2.png'
data.lastModified = new Date()
const myFile = new File([data], 'image2.png', {
type: data.type,
})
const buffer = await myFile.arrayBuffer()
const blobToBuffer = Buffer.from(buffer)
writeFile('image2.png', blobToBuffer, (err) => {
if (err) console.error(err.message)
console.log('File from Blob saved succesfully')
})
} catch (error) {
console.error(error.message)
}
}
fetchImageBlob()