-
Notifications
You must be signed in to change notification settings - Fork 1
/
tinifyCompressor.ts
128 lines (112 loc) · 3.35 KB
/
tinifyCompressor.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Base64 } from "https://deno.land/x/bb64/mod.ts";
import { isURL } from "https://deno.land/x/is_url/mod.ts";
interface TinifyConfig {
api_key: string;
storagePath?: string;
}
const { writeFile } = Deno;
class Tinify {
// AOI Key Declarion
private API_KEY: string;
// Storage path
private storagePath: string = "";
// API endpint
private apiEndpoint = "https://api.tinify.com/shrink";
// oject constructor
constructor(config: TinifyConfig) {
this.API_KEY = config.api_key;
if (config.storagePath) {
this.storagePath = config.storagePath;
}
}
/**
* genreate Authentication string for Basic scheme from api key
* @returns Base64 string
*/
private authString() {
return Base64.fromString(`api:${this.API_KEY}`).toString();
}
/**
* send the image to tinify API endpoint
* @param requestInit RequestInit
* @returns json
*/
private async compressImageRequest(requestInit: RequestInit) {
const response = await fetch(this.apiEndpoint, {
method: "POST",
...requestInit,
});
const jsonReponse = await response.json()
// throw Unauthorized Error whene Credentials are invalid. or success response
return response.ok ? Promise.resolve(jsonReponse) : Promise.reject(`${jsonReponse.error}: ${jsonReponse.message}`)
}
/**
* get buffer image and return Uint8Array Encode
* @param bufferImage ArrayBuffer
* @returns Uint8Array
*/
private bufferImageToUint8Array(bufferImage: ArrayBuffer) {
return new Uint8Array(bufferImage);
}
// get the image as arrayBuffer from URL
private async getImageFromURL(imageURL: string) {
const response = await fetch(imageURL);
return response.arrayBuffer();
}
public async compress(imageSource: Uint8Array | string) {
let response: any;
if (isURL(imageSource)) {
response = await this.compressImageRequest({
headers: {
"Authorization": `Basic ${this.authString()}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
"source": {
"url": imageSource,
},
}),
});
} else {
response = await this.compressImageRequest({
headers: {
"Authorization": `Basic ${this.authString()}`,
},
body: imageSource,
});
}
// get beffer compressed image from tinify API
const bufferCompressedImage: any = await this.getImageFromURL(
(await response).output.url,
);
return {
// seve the compersed image as file to the server
saveTo: async (fileName: string, storagePath?: string) => {
// create new image
await writeFile(
`${storagePath ? storagePath : this.storagePath}${fileName}`,
this.bufferImageToUint8Array(bufferCompressedImage),
);
return {
...await response,
compressedImagePath: `${
storagePath ? storagePath : this.storagePath
}${fileName}`,
};
},
/**
* convert the compresed image to base64 stribng
*/
toBase64: async () => {
const base64Image = Base64.fromUint8Array(
this.bufferImageToUint8Array(bufferCompressedImage),
).toString()
return {
...await response,
base64: base64Image,
};
}
};
}
}
export { Tinify };