-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
189 lines (159 loc) · 6.35 KB
/
index.html
File metadata and controls
189 lines (159 loc) · 6.35 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Photo Search</title>
<style>
body { font-family: sans-serif; max-width: 800px; margin: 0 auto; padding: 1rem; }
h1 { margin-bottom: 0.5rem; }
form { margin-bottom: 1.5rem; border: 1px solid #ddd; padding: 1rem; border-radius: 4px; }
label { display: block; margin-bottom: 0.25rem; }
input[type="text"], input[type="file"] { width: 100%; margin-bottom: 0.5rem; }
button { padding: 0.5rem 1rem; cursor: pointer; }
#results { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.75rem; }
.photo-card { border: 1px solid #eee; padding: 0.5rem; border-radius: 4px; text-align: center; }
.photo-card img { max-width: 100%; max-height: 150px; object-fit: cover; }
.small { font-size: 0.8rem; color: #555; }
</style>
</head>
<body>
<h1>Photo Search - Now Deployed with Amazon CodePipeline!</h1>
<!-- Search form -->
<form id="search-form">
<h2>Search Photos</h2>
<label for="search-query">Search query:</label>
<input id="search-query" type="text" placeholder="e.g. beach, dog, park" />
<button type="submit">Search</button>
</form>
<div id="results"></div>
<!-- Upload form -->
<form id="upload-form">
<h2>Upload Photo</h2>
<label for="photo-file">Photo file:</label>
<input id="photo-file" type="file" accept="image/*" required />
<label for="custom-labels">
Custom labels (comma-separated):
</label>
<input
id="custom-labels"
type="text"
placeholder="Sam, Sally"
/>
<button type="submit">Upload</button>
<div id="upload-status" class="small"></div>
</form>
<script src="sdk/apiGateway-js-sdk/lib/axios/dist/axios.standalone.js"></script>
<script src="sdk/apiGateway-js-sdk/lib/CryptoJS/rollups/crypto-js.js"></script>
<script src="sdk/apiGateway-js-sdk/lib/url-template/url-template.js"></script>
<script src="sdk/apiGateway-js-sdk/lib/apiGatewayCore/sigV4Client.js"></script>
<script src="sdk/apiGateway-js-sdk/lib/apiGatewayCore/apiGatewayClient.js"></script>
<script src="sdk/apiGateway-js-sdk/lib/apiGatewayCore/simpleHttpClient.js"></script>
<script src="sdk/apiGateway-js-sdk/lib/apiGatewayCore/utils.js"></script>
<script src="sdk/apiGateway-js-sdk/apigClient.js"></script>
<script>
// == CONFIG ==
const apigClient = apigClientFactory.newClient({
apiKey: 'ZVAEovpXhKbgt5GyXsid3ksNqKDoUf24EvG0BV27', // from API Gateway usage plan
region: 'us-east-1' // matches your API region
});
const PHOTO_BUCKET = 'photosbucket-346225466066-us-east-1'; // e.g. 'photosbucket-123456789012-us-east-1'
// Build public URL for an object in B2
function buildPhotoUrl(objectKey) {
// If using S3 website hosting instead of the REST endpoint, adjust this accordingly.
return `https://${PHOTO_BUCKET}.s3.amazonaws.com/${encodeURIComponent(objectKey)}`;
}
// === SEARCH ===
const searchForm = document.getElementById('search-form');
const searchInput = document.getElementById('search-query');
const resultsDiv = document.getElementById('results');
searchForm.addEventListener('submit', function (e) {
e.preventDefault();
const q = searchInput.value.trim();
if (!q) return;
resultsDiv.textContent = 'Searching...';
const params = { q }; // this becomes ?q=...
const body = {}; // GET has no body
const additionalParams = {}; // headers, etc., if needed
apigClient.searchGet(params, body, additionalParams)
.then(function (resp) {
// resp.data is the JSON response from LF2
const data = resp.data;
const results = data.results || data; // depending on how LF2 responds
if (!results.length) {
resultsDiv.textContent = 'No photos found.';
return;
}
resultsDiv.innerHTML = '';
results.forEach(photo => {
const card = document.createElement('div');
card.className = 'photo-card';
const img = document.createElement('img');
img.src = buildPhotoUrl(photo.objectKey); // write this helper using your photo bucket
const info = document.createElement('div');
info.className = 'small';
info.textContent = `${photo.objectKey} (${(photo.labels || []).join(', ')})`;
card.appendChild(img);
card.appendChild(info);
resultsDiv.appendChild(card);
});
})
.catch(function (err) {
console.error(err);
resultsDiv.textContent = 'Error performing search. Check console.';
});
});
// === UPLOAD ===
const uploadForm = document.getElementById('upload-form');
const fileInput = document.getElementById('photo-file');
const customLabelsInput = document.getElementById('custom-labels');
const uploadStatus = document.getElementById('upload-status');
const API_BASE = 'https://98klk5rvd1.execute-api.us-east-1.amazonaws.com/Stage1';
uploadForm.addEventListener('submit', async function (e) {
e.preventDefault();
uploadStatus.textContent = '';
const file = fileInput.files[0];
if (!file) {
uploadStatus.textContent = 'Please choose a file.';
return;
}
console.log("Selected file:", file.name, "size:", file.size);
const raw = customLabelsInput.value.trim();
let customLabelsHeader = '';
if (raw) {
const labels = raw
.split(',')
.map(l => l.trim())
.filter(l => l.length > 0);
customLabelsHeader = labels.join(', ');
}
const objectKey = file.name;
const url = `${API_BASE}/photos/${encodeURIComponent(objectKey)}`;
const headers = {
'Content-Type': file.type || 'application/octet-stream',
'x-amz-meta-customlabels': customLabelsHeader || ''
};
uploadStatus.textContent = 'Uploading...';
try {
const resp = await fetch(url, {
method: 'PUT',
headers,
body: file // ⬅️ this is what actually sends the bytes
});
console.log('Upload response status:', resp.status);
if (!resp.ok) {
const text = await resp.text().catch(() => '');
console.error('Upload failed:', resp.status, text);
uploadStatus.textContent = `Upload failed (${resp.status}). Check console.`;
return;
}
uploadStatus.textContent = 'Upload successful!';
fileInput.value = '';
customLabelsInput.value = '';
} catch (err) {
console.error('Upload error:', err);
uploadStatus.textContent = 'Error uploading photo. Check console.';
}
});
</script>
</body>
</html>