forked from Mhassanbughio/-AR---AI-Cut-Paste-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer
58 lines (48 loc) · 1.17 KB
/
Server
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
import Base64 from "./Base64";
const URL = "http://192.168.1.29:8080";
function arrayBufferToBase64(buffer: ArrayBuffer) {
let binary = "";
const bytes = [].slice.call(new Uint8Array(buffer));
bytes.forEach((b) => (binary += String.fromCharCode(b)));
return Base64.btoa(binary);
}
function ping() {
fetch(URL + "/ping");
}
async function cut(imageURI: string) {
const formData = new FormData();
formData.append("data", {
uri: imageURI,
name: "photo",
type: "image/jpg",
});
const resp = await fetch(URL + "/cut", {
method: "POST",
body: formData,
}).then(async (res) => {
console.log("> converting...");
const buffer = await res.arrayBuffer();
const base64Flag = "data:image/png;base64,";
const imageStr = arrayBufferToBase64(buffer);
return base64Flag + imageStr;
});
return resp;
}
async function paste(imageURI: string) {
const formData = new FormData();
formData.append("data", {
uri: imageURI,
name: "photo",
type: "image/jpg",
});
const resp = await fetch(URL + "/paste", {
method: "POST",
body: formData,
}).then((r) => r.json());
return resp;
}
export default {
ping,
cut,
paste,
};