-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
159 lines (135 loc) · 5.84 KB
/
app.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(function ($) {
let functionUrlPresign = localStorage.getItem("functionUrlPresign");
if (functionUrlPresign) {
$("#functionUrlPresign").val(functionUrlPresign);
}
let functionUrlList = localStorage.getItem("functionUrlList");
if (functionUrlList) {
console.log("function url list is", functionUrlList);
$("#functionUrlList").val(functionUrlList);
}
let imageItemTemplate = Handlebars.compile($("#image-item-template").html());
$("#configForm").submit(async function (event) {
if (event.preventDefault)
event.preventDefault();
else
event.returnValue = false;
event.preventDefault();
let action = $(this).find("button[type=submit]:focus").attr('name');
if (action === undefined) {
// the jquery find with the focus does not work on Safari, maybe because the focus is not instantly given
// fallback to manually retrieving the submitter from the original event
action = event.originalEvent.submitter.getAttribute('name')
}
if (action == "load") {
let baseUrl = `${document.location.protocol}//${document.location.host}`;
if (baseUrl.indexOf("file://") >= 0) {
baseUrl = `http://localhost:4566`;
}
baseUrl = baseUrl.replace("://webapp.s3.", "://").replace("://webapp.s3-website.", "://");
const headers = {authorization: "AWS4-HMAC-SHA256 Credential=test/20231004/us-east-1/lambda/aws4_request, ..."};
const loadUrl = async (funcName, resultElement) => {
const url = `${baseUrl}/2021-10-31/functions/${funcName}/urls`;
const result = await $.ajax({url, headers}).promise();
const funcUrl = JSON.parse(result).FunctionUrlConfigs[0].FunctionUrl;
$(`#${resultElement}`).val(funcUrl);
localStorage.setItem(resultElement, funcUrl);
}
await loadUrl("presign", "functionUrlPresign");
await loadUrl("list", "functionUrlList");
alert("Function URL configurations loaded");
} else if (action == "save") {
localStorage.setItem("functionUrlPresign", $("#functionUrlPresign").val());
localStorage.setItem("functionUrlList", $("#functionUrlList").val());
alert("Configuration saved");
} else if (action == "clear") {
localStorage.removeItem("functionUrlPresign");
localStorage.removeItem("functionUrlList");
$("#functionUrlPresign").val("")
$("#functionUrlList").val("")
alert("Configuration cleared");
} else {
alert("Unknown action");
}
});
$("#uploadForm").submit(function (event) {
$("#uploadForm button").addClass('disabled');
if (event.preventDefault)
event.preventDefault();
else
event.returnValue = false;
event.preventDefault();
let fileName = $("#customFile").val().replace(/C:\\fakepath\\/i, '');
let functionUrlPresign = $("#functionUrlPresign").val();
// modify the original form
console.log(fileName, functionUrlPresign);
let urlToCall = functionUrlPresign + "/" + fileName
console.log(urlToCall);
$.ajax({
url: urlToCall,
success: function (data) {
console.log("got pre-signed POST URL", data);
let fields = data['fields'];
let formData = new FormData()
Object.entries(fields).forEach(([field, value]) => {
formData.append(field, value);
});
// the file <input> element, "file" needs to be the last element of the form
const fileElement = document.querySelector("#customFile");
formData.append("file", fileElement.files[0]);
console.log("sending form data", formData);
$.ajax({
type: "POST",
url: data['url'],
data: formData,
processData: false,
contentType: false,
success: function () {
alert("success!");
updateImageList();
},
error: function () {
alert("error! check the logs");
},
complete: function (event) {
console.log("done", event);
$("#uploadForm button").removeClass('disabled');
}
});
},
error: function (e) {
console.log("error", e);
alert("error getting pre-signed URL. check the logs!");
$("#uploadForm button").removeClass('disabled');
}
});
});
function updateImageList() {
let listUrl = $("#functionUrlList").val();
if (!listUrl) {
alert("Please set the function URL of the list Lambda");
return
}
$.ajax({
url: listUrl,
success: function (response) {
$('#imagesContainer').empty(); // Empty imagesContainer
response.forEach(function (item) {
console.log(item);
let cardHtml = imageItemTemplate(item);
$("#imagesContainer").append(cardHtml);
});
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("Error:", textStatus, errorThrown);
alert("error! check the logs");
}
});
}
$("#updateImageListButton").click(function (event) {
updateImageList();
});
if (functionUrlList) {
updateImageList();
}
})(jQuery);