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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
import "fs";
import React = require("react");
import { Doc, DocListCast, DocListCastAsync, Opt } from "../../../new_fields/Doc";
import { RouteStore } from "../../../server/RouteStore";
import { action, observable, autorun, runInAction, computed, reaction, IReactionDisposer } from "mobx";
import { FieldViewProps, FieldView } from "../../views/nodes/FieldView";
import Measure, { ContentRect } from "react-measure";
import { library } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTag, faPlus, faCloudUploadAlt } from '@fortawesome/free-solid-svg-icons';
import { Docs, DocumentOptions } from "../../documents/Documents";
import { observer } from "mobx-react";
import ImportMetadataEntry, { keyPlaceholder, valuePlaceholder } from "./ImportMetadataEntry";
import { Utils } from "../../../Utils";
import { DocumentManager } from "../DocumentManager";
import { Id } from "../../../new_fields/FieldSymbols";
import { List } from "../../../new_fields/List";
import { Cast, BoolCast, NumCast } from "../../../new_fields/Types";
import { listSpec } from "../../../new_fields/Schema";
import { GooglePhotos } from "../../apis/google_docs/GooglePhotosClientUtils";
import { SchemaHeaderField } from "../../../new_fields/SchemaHeaderField";
import "./DirectoryImportBox.scss";
import { Identified } from "../../Network";
import { BatchedArray } from "array-batcher";
import { ExifData } from "exif";
const unsupported = ["text/html", "text/plain"];
interface ImageUploadResponse {
name: string;
path: string;
type: string;
exif: any;
}
@observer
export default class DirectoryImportBox extends React.Component<FieldViewProps> {
private selector = React.createRef<HTMLInputElement>();
@observable private top = 0;
@observable private left = 0;
private dimensions = 50;
@observable private phase = "";
private disposer: Opt<IReactionDisposer>;
@observable private entries: ImportMetadataEntry[] = [];
@observable private quota = 1;
@observable private completed = 0;
@observable private uploading = false;
@observable private removeHover = false;
public static LayoutString() { return FieldView.LayoutString(DirectoryImportBox); }
constructor(props: FieldViewProps) {
super(props);
library.add(faTag, faPlus);
let doc = this.props.Document;
this.editingMetadata = this.editingMetadata || false;
this.persistent = this.persistent || false;
!Cast(doc.data, listSpec(Doc)) && (doc.data = new List<Doc>());
}
@computed
private get editingMetadata() {
return BoolCast(this.props.Document.editingMetadata);
}
private set editingMetadata(value: boolean) {
this.props.Document.editingMetadata = value;
}
@computed
private get persistent() {
return BoolCast(this.props.Document.persistent);
}
private set persistent(value: boolean) {
this.props.Document.persistent = value;
}
handleSelection = async (e: React.ChangeEvent<HTMLInputElement>) => {
runInAction(() => {
this.uploading = true;
this.phase = "Initializing download...";
});
let docs: Doc[] = [];
let files = e.target.files;
if (!files || files.length === 0) return;
let directory = (files.item(0) as any).webkitRelativePath.split("/", 1)[0];
let validated: File[] = [];
for (let i = 0; i < files.length; i++) {
let file = files.item(i);
file && !unsupported.includes(file.type) && validated.push(file);
}
runInAction(() => {
this.quota = validated.length;
this.completed = 0;
});
let sizes: number[] = [];
let modifiedDates: number[] = [];
runInAction(() => this.phase = `Internal: uploading ${this.quota - this.completed} files to Dash...`);
const uploads = await BatchedArray.from(validated, { batchSize: 15 }).batchedMapAsync(async batch => {
const formData = new FormData();
batch.forEach(file => {
sizes.push(file.size);
modifiedDates.push(file.lastModified);
formData.append(Utils.GenerateGuid(), file);
});
const responses = await Identified.PostFormDataToServer(RouteStore.upload, formData);
runInAction(() => this.completed += batch.length);
return responses as ImageUploadResponse[];
});
await Promise.all(uploads.map(async upload => {
const type = upload.type;
const path = Utils.prepend(upload.path);
const options = {
nativeWidth: 300,
width: 300,
title: upload.name
};
const document = await Docs.Get.DocumentFromType(type, path, options);
const { data, error } = upload.exif;
if (document) {
Doc.GetProto(document).exif = error || Docs.Get.DocumentHierarchyFromJson(data);
docs.push(document);
}
}));
for (let i = 0; i < docs.length; i++) {
let doc = docs[i];
doc.size = sizes[i];
doc.modified = modifiedDates[i];
this.entries.forEach(entry => {
let target = entry.onDataDoc ? Doc.GetProto(doc) : doc;
target[entry.key] = entry.value;
});
}
let doc = this.props.Document;
let height: number = NumCast(doc.height) || 0;
let offset: number = this.persistent ? (height === 0 ? 0 : height + 30) : 0;
let options: DocumentOptions = {
title: `Import of ${directory}`,
width: 1105,
height: 500,
x: NumCast(doc.x),
y: NumCast(doc.y) + offset
};
let parent = this.props.ContainingCollectionView;
if (parent) {
let importContainer: Doc;
if (docs.length < 50) {
importContainer = Docs.Create.MasonryDocument(docs, options);
} else {
const headers = [new SchemaHeaderField("title"), new SchemaHeaderField("size")];
importContainer = Docs.Create.SchemaDocument(headers, docs, options);
}
runInAction(() => this.phase = 'External: uploading files to Google Photos...');
importContainer.singleColumn = false;
await GooglePhotos.Export.CollectionToAlbum({ collection: importContainer });
Doc.AddDocToList(Doc.GetProto(parent.props.Document), "data", importContainer);
!this.persistent && this.props.removeDocument && this.props.removeDocument(doc);
DocumentManager.Instance.jumpToDocument(importContainer, true);
}
runInAction(() => {
this.uploading = false;
this.quota = 1;
this.completed = 0;
});
}
componentDidMount() {
this.selector.current!.setAttribute("directory", "");
this.selector.current!.setAttribute("webkitdirectory", "");
this.disposer = reaction(
() => this.completed,
completed => runInAction(() => this.phase = `Internal: uploading ${this.quota - completed} files to Dash...`)
);
}
componentWillUnmount() {
this.disposer && this.disposer();
}
@action
preserveCentering = (rect: ContentRect) => {
let bounds = rect.offset!;
if (bounds.width === 0 || bounds.height === 0) {
return;
}
let offset = this.dimensions / 2;
this.left = bounds.width / 2 - offset;
this.top = bounds.height / 2 - offset;
}
@action
addMetadataEntry = async () => {
let entryDoc = new Doc();
entryDoc.checked = false;
entryDoc.key = keyPlaceholder;
entryDoc.value = valuePlaceholder;
Doc.AddDocToList(this.props.Document, "data", entryDoc);
}
@action
remove = async (entry: ImportMetadataEntry) => {
let metadata = await DocListCastAsync(this.props.Document.data);
if (metadata) {
let index = this.entries.indexOf(entry);
if (index !== -1) {
runInAction(() => this.entries.splice(index, 1));
index = metadata.indexOf(entry.props.Document);
if (index !== -1) {
metadata.splice(index, 1);
}
}
}
}
render() {
let dimensions = 50;
let entries = DocListCast(this.props.Document.data);
let isEditing = this.editingMetadata;
let completed = this.completed;
let quota = this.quota;
let uploading = this.uploading;
let showRemoveLabel = this.removeHover;
let persistent = this.persistent;
let percent = `${completed / quota * 100}`;
percent = percent.split(".")[0];
percent = percent.startsWith("100") ? "99" : percent;
let marginOffset = (percent.length === 1 ? 5 : 0) - 1.6;
const message = <span className={"phase"}>{this.phase}</span>;
const centerPiece = this.phase.includes("Google Photos") ?
<img src={"/assets/google_photos.png"} style={{
transition: "0.4s opacity ease",
width: 30,
height: 30,
opacity: uploading ? 1 : 0,
pointerEvents: "none",
position: "absolute",
left: 12,
top: this.top + 10,
fontSize: 18,
color: "white",
marginLeft: this.left + marginOffset
}} />
: <div
style={{
transition: "0.4s opacity ease",
opacity: uploading ? 1 : 0,
pointerEvents: "none",
position: "absolute",
left: 10,
top: this.top + 12.3,
fontSize: 18,
color: "white",
marginLeft: this.left + marginOffset
}}>{percent}%</div>;
return (
<Measure offset onResize={this.preserveCentering}>
{({ measureRef }) =>
<div ref={measureRef} style={{ width: "100%", height: "100%", pointerEvents: "all" }} >
{message}
<input
id={"selector"}
ref={this.selector}
onChange={this.handleSelection}
type="file"
style={{
position: "absolute",
display: "none"
}} />
<label
htmlFor={"selector"}
style={{
opacity: isEditing ? 0 : 1,
pointerEvents: isEditing ? "none" : "all",
transition: "0.4s ease opacity"
}}
>
<div style={{
width: dimensions,
height: dimensions,
borderRadius: "50%",
background: "black",
position: "absolute",
left: this.left,
top: this.top
}} />
<div style={{
position: "absolute",
left: this.left + 8,
top: this.top + 10,
opacity: uploading ? 0 : 1,
transition: "0.4s opacity ease"
}}>
<FontAwesomeIcon icon={faCloudUploadAlt} color="#FFFFFF" size={"2x"} />
</div>
<img
style={{
width: 80,
height: 80,
transition: "0.4s opacity ease",
opacity: uploading ? 0.7 : 0,
position: "absolute",
top: this.top - 15,
left: this.left - 15
}}
src={"/assets/loading.gif"}></img>
</label>
<input
type={"checkbox"}
onChange={e => runInAction(() => this.persistent = e.target.checked)}
style={{
margin: 0,
position: "absolute",
left: 10,
bottom: 10,
opacity: isEditing || uploading ? 0 : 1,
transition: "0.4s opacity ease",
pointerEvents: isEditing || uploading ? "none" : "all"
}}
checked={this.persistent}
onPointerEnter={action(() => this.removeHover = true)}
onPointerLeave={action(() => this.removeHover = false)}
/>
<p
style={{
position: "absolute",
left: 27,
bottom: 8.4,
fontSize: 12,
opacity: showRemoveLabel ? 1 : 0,
transition: "0.4s opacity ease"
}}>Template will be <span style={{ textDecoration: "underline", textDecorationColor: persistent ? "green" : "red", color: persistent ? "green" : "red" }}>{persistent ? "kept" : "removed"}</span> after upload</p>
{centerPiece}
<div
style={{
position: "absolute",
top: 10,
right: 10,
borderRadius: "50%",
width: 25,
height: 25,
background: "black",
pointerEvents: uploading ? "none" : "all",
opacity: uploading ? 0 : 1,
transition: "0.4s opacity ease"
}}
title={isEditing ? "Back to Upload" : "Add Metadata"}
onClick={action(() => this.editingMetadata = !this.editingMetadata)}
/>
<FontAwesomeIcon
style={{
pointerEvents: "none",
position: "absolute",
right: isEditing ? 14 : 15,
top: isEditing ? 15.4 : 16,
opacity: uploading ? 0 : 1,
transition: "0.4s opacity ease"
}}
icon={isEditing ? faCloudUploadAlt : faTag}
color="#FFFFFF"
size={"1x"}
/>
<div
style={{
transition: "0.4s ease opacity",
width: "100%",
height: "100%",
pointerEvents: isEditing ? "all" : "none",
opacity: isEditing ? 1 : 0,
overflowY: "scroll"
}}
>
<div
style={{
borderRadius: "50%",
width: 25,
height: 25,
marginLeft: 10,
position: "absolute",
right: 41,
top: 10
}}
title={"Add Metadata Entry"}
onClick={this.addMetadataEntry}
>
<FontAwesomeIcon
style={{
pointerEvents: "none",
marginLeft: 6.4,
marginTop: 5.2
}}
icon={faPlus}
size={"1x"}
/>
</div>
<p style={{ paddingLeft: 10, paddingTop: 8, paddingBottom: 7 }} >Add metadata to your import...</p>
<hr style={{ margin: "6px 10px 12px 10px" }} />
{entries.map(doc =>
<ImportMetadataEntry
Document={doc}
key={doc[Id]}
remove={this.remove}
ref={(el) => { if (el) this.entries.push(el); }}
next={this.addMetadataEntry}
/>
)}
</div>
</div>
}
</Measure>
);
}
}
|