-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
143 lines (121 loc) · 4.77 KB
/
upload.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
document.getElementById("displaytext").style.display = "none";
function searchPhoto() {
var apigClient = apigClientFactory.newClient({
apiKey: 'gnavV2E0Pz5Kit0HSnfqL8loqijjhvHH7gZTVV9Z'
});
var image_message = document.getElementById("note-textarea").value;
if(image_message == "")
var image_message = document.getElementById("transcript").value;
console.log(image_message);
var body = {};
var params = {
q: image_message,
'x-api-key': 'gnavV2E0Pz5Kit0HSnfqL8loqijjhvHH7gZTVV9Z'
};
var additionalParams = {
headers: {
'Content-Type': "application/json",
},
};
apigClient.searchGet(params, body, additionalParams)
.then(function (result) {
//This is where you would put a success callback
response_data = result.data
var img1 = result.data.body;
length_of_response = response_data.length;
if (length_of_response == 0) {
document.getElementById("displaytext").innerHTML = "No Images Found !!!"
document.getElementById("displaytext").style.display = "block";
}
img1 = img1.replace(/\"/g, "").replace("[", "").replace("]", "");
img1 = img1.split(",");
document.getElementById("img-container").innerHTML = "";
var para = document.createElement("p");
para.setAttribute("id", "displaytext")
document.getElementById("img-container").appendChild(para);
img1.forEach(function (obj) {
var img = new Image();
// img.src = "https://photosforsearch1.s3.amazonaws.com/"+obj;
img.src = obj;
img.setAttribute("class", "banner-img");
img.setAttribute("alt", "effy");
img.setAttribute("width", "150");
img.setAttribute("height", "100");
document.getElementById("displaytext").innerHTML = "Images returned are : "
document.getElementById("img-container").appendChild(img);
document.getElementById("displaytext").style.display = "block";
});
}).catch(function (result) {
//This is where you would put an error callback
});
}
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
// reader.onload = () => resolve(reader.result)
reader.onload = () => {
let encoded = reader.result.replace(/^data:(.*;base64,)?/, '');
if ((encoded.length % 4) > 0) {
encoded += '='.repeat(4 - (encoded.length % 4));
}
resolve(encoded);
};
reader.onerror = error => reject(error);
});
}
function uploadPhoto() {
// var file_data = $("#file_path").prop("files")[0];
var file = document.getElementById('file_path').files[0];
const reader = new FileReader();
var file_data;
// var file = document.querySelector('#file_path > input[type="file"]').files[0];
var encoded_image = getBase64(file).then(
data => {
console.log(data)
var apigClient = apigClientFactory.newClient({
apiKey: "gnavV2E0Pz5Kit0HSnfqL8loqijjhvHH7gZTVV9Z",
defaultContentType: "image/jpeg",
defaultAcceptType: "image/jpeg"
});
// var data = document.getElementById('file_path').value;
// var x = data.split("\\")
// var filename = x[x.length-1]
// var file_type = file.type + ";base64"
// var body = data;
var body = file;
var params = {
"item": file.name,
"folder": "photosforsearch1",
'x-api-key': 'gnavV2E0Pz5Kit0HSnfqL8loqijjhvHH7gZTVV9Z'
};
var additionalParams = {
headers: {
'Content-Type': "image/jpeg",
},
};
// apigClient.uploadFolderItemPut(params, body, additionalParams).then(function (res) {
// if (res.status == 200) {
// // alert("Upload Successfull")
// console.log("Success");
// document.getElementById("uploadText").innerHTML = "IMAGE UPLOADED SUCCESSFULLY !!!"
// document.getElementById("uploadText").style.display = "block";
// document.getElementById("uploadText").style.color = "green";
// document.getElementById("uploadText").style.fontWeight = "bold";
// }
// })
});
let config = {
headers: { 'Content-Type': file.type }
};
url = 'https://cors-anywhere.herokuapp.com/https://q6mpc0sjz1.execute-api.us-east-1.amazonaws.com/TestAuth/upload/photosforsearch1/' + file.name
axios.put(url, file, config).then(response => {
// console.log(" New "+response.data)
// alert("Image uploaded successfully!");
console.log("Success");
document.getElementById("uploadText").innerHTML = "IMAGE UPLOADED SUCCESSFULLY !!!"
document.getElementById("uploadText").style.display = "block";
document.getElementById("uploadText").style.color = "green";
document.getElementById("uploadText").style.fontWeight = "bold";
});
}