-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
60 lines (49 loc) · 2.11 KB
/
index.ts
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
import { Context } from "https://deno.land/x/[email protected]/mod.ts"
import * as inputValidation from "./inputValidation.ts"
import * as cache from "./cache.ts"
import * as hash from "./hash.ts"
import * as keyword from "./keyword.ts"
import * as pdf_handler from "./pdf_handler.ts"
import * as pdf_to_jpg from "./pdf_to_jpg.ts"
export async function pdfImageConverter(pdfLocation: string, pageToConvert: string){
const antiOverlapNum = Math.random()
const pdfHash = hash.createHash(pdfLocation)
const value_firstToNum = keyword.firstToNum(pageToConvert)
let pageNum = -1
if (value_firstToNum !== false) {
pageNum = value_firstToNum
}
const value_checkCache = await cache.checkCache(pdfHash, pageToConvert)
if (value_checkCache !== false){
return value_checkCache
}
await pdf_handler.fetchPDF(pdfLocation, antiOverlapNum, pdfHash)
const pdfLength = await pdf_handler.getPDFLength(pdfHash, antiOverlapNum)
const value_keywordToPageNum = keyword.keywordToPageNum(pageToConvert, pdfLength)
if (value_keywordToPageNum !== false) {
pageNum = value_keywordToPageNum
}
if (pageNum > 0 && pageNum <= pdfLength){
await pdf_to_jpg.pdfToJPG(String(pageNum), pdfHash, antiOverlapNum, pageToConvert, String(pdfLength))
await pdf_handler.removeTemp(pdfHash, antiOverlapNum)
}
else {
await pdf_handler.removeTemp(pdfHash, antiOverlapNum)
throw new Error("Page outside of valid range")
}
const value_checkCache2 = await cache.checkCache(pdfHash, pageToConvert)
if (value_checkCache2 !== false) {
return value_checkCache2
}
else {
throw new Error("Conversion unsuccessful")
}
}
export const pdfImageConverterOakRoute = async (context:Context) => {
const {searchParams} = context.request.url
const {pdf, page} = inputValidation.validateInput(searchParams)
const file = await pdfImageConverter(pdf, page.toString())
context.response.headers.set('Content-Type', 'image/jpg');
context.response.headers.set('Access-Control-Allow-Origin', '*');
context.response.body = file
}