-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (81 loc) · 2.31 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const Router = require('./router')
const apiHandler = require('./api')
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const BASE = 'archillect.com'
const buildURL = url => {
const urlObj = new URL(url)
const newURL = urlObj.href.replace(urlObj.host, BASE)
return newURL
}
/**
* Response with the raw image
* @param {Request} request
*/
async function imgDownloaderHandler(request) {
const { url } = request
const parts = url.split('/')
if (parts.length !== 5 || !parts[4].includes('img')) {
// this is a regular request. forward it.
return fetch(buildURL(url))
}
// otherwise take it a content respose, we need to fetch it and return the image
const dropURL = parts.slice(0, 4).join('/');
const dropResponse = await fetch(buildURL(dropURL))
const rewriter = new HTMLRewriter()
let imagePath = ''
await rewriter
.on('meta[property="og:image"]', {
element(element) {
imagePath = element.getAttribute('content')
},
})
.transform(dropResponse)
.text();
return fetch(imagePath)
// return new Response(JSON.stringify(parts), {
// headers: { 'content-type': 'text/plain' },
// })
}
/**
* Adds a download button to image #sources section
* @param {Request} request
*/
async function imgHandler(request) {
const { url } = request
const parts = url.split('/')
const dropResponse = await fetch(buildURL(url))
const rewriter = new HTMLRewriter()
return rewriter
.on('#sources', {
element(element) {
element.prepend(`<a style="width: auto;padding: 0 1em;" href="/${parts[3]}/img">Download</a>`,
{ html: true },
)
},
})
.transform(dropResponse)
}
/**
* Adds a download button to image #sources section
* @param {Request} request
*/
async function mainHandler(request) {
const { url } = request
return fetch(buildURL(url))
}
/**
* main Handler which controls the routes
* @param {Request} request
*/
async function handleRequest(request) {
const r = new Router()
// Replace with the approriate paths and handlers
r.get('/api/.*', () => apiHandler(request))
r.get('/.[0-9]*/img', () => imgDownloaderHandler(request))
r.get('/.[0-9]*', () => imgHandler(request))
r.get('/.*', () => mainHandler(request))
const resp = await r.route(request)
return resp
}