-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal_upload.svelte
More file actions
227 lines (202 loc) · 7.62 KB
/
modal_upload.svelte
File metadata and controls
227 lines (202 loc) · 7.62 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<svelte:options accessors/>
<script>
// Modal for uploading files, with extra functionality for images (preview, resize-before-upload)
import Modal from './modal.svelte';
import { getStorage, ref, uploadBytesResumable, getDownloadURL } from 'firebase/storage';
import { FileSizeStr, Now, UUID } from './utils.js';
import { SetDoc, UpdateDoc, DeleteDoc } from './database.js';
export let overwriteID = null; // in: if provided, upload will override the existing File entry
export let uploadedFile = null; // out: on successful upload, the File object from the DB
/*
TODO
- when overwriting a file, if original was an image, require an image
*/
let uploading = false;
function OnClose(withCode)
{
return withCode == MODAL_OK || withCode == MODAL_CANCEL || !uploading;
}
let canvasEl;
let filesEl = null;
let inputImg = new Image();
let inputInfo = {isImage:false, width:0, height:0, ar:1, filename:'', size:0, sizeStr:'0 KB'};
inputImg.onload = e=>
{
URL.revokeObjectURL(e.target.src); // free the mem
inputInfo.width = inputImg.width;
inputInfo.height = inputImg.height;
inputInfo.ar = inputImg.width / inputImg.height;
ResetResize();
ResetCanvasResize();
}
function OnUploadInputChange()
{
let files = filesEl.files;
if (!files.length)
return
let file = files[0];
// use an image element to pull the data off disk and so we can inspect it for resizing
rawBlob = new Blob([file], {'type':file.type});
inputInfo.filename = file.name;
inputInfo.size = file.size;
inputInfo.sizeStr = FileSizeStr(file.size);
inputInfo.isImage = file.type.startsWith('image/');
if (inputInfo.isImage)
inputImg.src = URL.createObjectURL(rawBlob);
}
let resizeW = 0, resizeH = 0, displayW=500;
let lastResizeW = -1, lastResizeH = -1;
let curAR = 1;
let rawBlob = null; // the original file data, as a Blob
let compressedBlob = null; // the scaled, compressed image, as a Blob
let compressedSizeStr = '';
$:readyForUpload = (inputInfo.size && (!inputInfo.isImage || !!compressedBlob));
$:if (resizeW != -1 && resizeH != -1) TriggerResize();
$:if (displayW != -1) TriggerCanvasResize();
// called each time the resize inputs are changed
function TriggerResize()
{
if (!inputInfo.isImage) return;
// if user changed W, auto-calc H, and vice versa
if (resizeW != lastResizeW)
resizeH = Math.max(1, Math.round(resizeW / inputInfo.ar));
else if (resizeH != lastResizeH)
resizeW = Math.max(1, Math.round(resizeH * inputInfo.ar));
if (resizeW <= 0 || resizeH <= 0 || displayW <= 0)
return;
compressedBlob = null;
lastResizeW = resizeW;
lastResizeH = resizeH;
curAR = resizeW / resizeH;
TriggerCanvasResize();
createImageBitmap(inputImg, {resizeQuality:'high', resizeWidth:resizeW, resizeHeight:resizeH}).then(img =>
{
canvasEl.width = resizeW;
canvasEl.height = resizeH;
let ctx = canvasEl.getContext('2d');
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
ctx.drawImage(img, 0, 0);
canvasEl.toBlob(res =>
{
compressedSizeStr = FileSizeStr(res.size);
compressedBlob = res;
}, 'image/jpeg');
})
}
function TriggerCanvasResize()
{
if (canvasEl && curAR > 0)
{
canvasEl.style.width = displayW + 'px';
canvasEl.style.height = Math.round(displayW/curAR) + 'px';
}
}
// restores resize parameters to those of the original image
function ResetResize()
{
resizeW = lastResizeW = inputInfo.width;
resizeH = lastResizeH = inputInfo.height;
}
function ResetCanvasResize()
{
displayW = Math.max(10, Math.min(resizeW, 500));
}
function DoUpload()
{
uploading = true;
OpenBusyModal('Uploading file', true); // true=show progress bar
let uuid = overwriteID ? overwriteID : UUID(); // TODO: if provided, look for that File obj to preserve its info, e.g. .created (maybe we don't care?)
// because we're hitting two different resources (Firestore and Google Storage), we can't both create the DB record and
// upload the file in a single transaction, so we create the DB record with uploadDone=false, then upload the file, then
// update uploadDone=true in the DB, deleting that DB record if the upload got aborted.
let now = Now();
let ext = inputInfo.filename.split('.').pop().toLowerCase();
let storagePath = `assets/${uuid}.${ext}`;
console.log('Uploading to', storagePath);
let url = '';
let type = inputInfo.isImage ? 'image' : 'file';
let args = {created:now, lastmod:now, orig:inputInfo.filename, storagePath, size:inputInfo.size, type, url,
w:inputInfo.width, h:inputInfo.height, uploadDone:false};
SetDoc('File', uuid, args).then(() =>
{
console.log('DB record created, starting upload');
let storageRef = ref(getStorage(), storagePath);
let uploadTask = uploadBytesResumable(storageRef, inputInfo.isImage ? compressedBlob : rawBlob);
uploadTask.on('state_changed', snapshot =>
{
UpdateModal({percent:100 * snapshot.bytesTransferred / snapshot.totalBytes});
},
err =>
{
console.log('Upload error:', err);
DeleteDoc('File', uuid).then(() =>
{
uploading = false;
OpenAlertModal('The upload failed; please try again.', 'Error');
});
},
() =>
{ // upload finished successfully, so mark the upload as done and close the modal
UpdateModal({percent:100});
console.log('Upload completed successfully; marked upload complete in DB.');
getDownloadURL(uploadTask.snapshot.ref).then(url =>
{
UpdateDoc('File', uuid, {uploadDone:true, url}).then(() =>
{
CloseModal(); // this closes the progress modal
uploadedFile = Object.assign(args, {id:uuid, uploadDone:true, url});
CloseModal(); // this closes the upload modal
});
});
});
});
}
</script>
<Modal close={OnClose}>
<h2>Upload a file</h2>
<content>
<form name="uploadForm">
<input id="uploadInput" type="file" disabled={uploading} bind:this={filesEl} on:change={OnUploadInputChange}>
</form>
{#if inputInfo.size}
{#if inputInfo.isImage}
<hbox class="center">Original: {inputInfo.sizeStr}, {inputInfo.width} x {inputInfo.height}</hbox>
<hbox class="center">
Resized: {compressedSizeStr},
<input class="num" type="text" disabled={uploading} bind:value={resizeW}/> x <input class="num" type="text" bind:value={resizeH} disabled={uploading} />
<button disabled={uploading} on:click={ResetResize}>Reset</button>
</hbox>
<br/>
<canvas bind:this={canvasEl} />
<hbox class="center">
Preview width: <input type="text" class="num" bind:value={displayW}/>
<button on:click={ResetCanvasResize}>Reset</button>
</hbox>
{:else}
<!-- if it's not an image, there's nothing else to show -->
{/if}
{/if}
<hr/>
<hbox>
<button on:click={()=>CloseModal(MODAL_CANCEL)} disabled={uploading}>Cancel</button>
<filler/>
<button on:click={DoUpload} disabled={!readyForUpload||uploading}>Upload</button>
</hbox>
</content>
</Modal>
<style>
content {
min-width:600px;
}
canvas {
width:100%;
height:300;
border:solid black 1px;
}
input.num {
width:6ch;
margin-left:3px;
margin-right:3px;
height:2.6ch;
}
</style>