-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphoto-upload.component.ts
116 lines (101 loc) · 3.87 KB
/
photo-upload.component.ts
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
import {Component, OnInit, Input, NgZone} from '@angular/core';
import {FileUploader, FileUploaderOptions, ParsedResponseHeaders} from 'ng2-file-upload';
import {Cloudinary} from '@cloudinary/angular';
@Component({
selector: 'photo-upload',
templateUrl: './app/photo-upload/photo-upload.component.html'
})
export class PhotoUploadComponent implements OnInit {
@Input()
responses: Array<any>;
private hasBaseDropZoneOver: boolean = false;
private uploader: FileUploader;
private title: string;
constructor(
private cloudinary: Cloudinary,
private zone: NgZone
) {
this.responses = [];
this.title = '';
}
ngOnInit(): void {
const uploaderOptions: FileUploaderOptions = {
url: `https://api.cloudinary.com/v1_1/${this.cloudinary.config().cloud_name}/upload`,
autoUpload: true,
isHTML5: true,
removeAfterUpload: true,
headers: [
{
name: 'X-Requested-With',
value: 'XMLHttpRequest'
}
]
};
this.uploader = new FileUploader(uploaderOptions);
// Add custom tag for displaying the uploaded photo in the list
this.uploader.onBuildItemForm = (fileItem: any, form: FormData): any => {
form.append('upload_preset', this.cloudinary.config().upload_preset);
let tags = 'myphotoalbum';
if (this.title) {
form.append('context', `photo=${this.title}`);
tags = `myphotoalbum,${this.title}`;
}
form.append('tags', tags);
form.append('file', fileItem);
fileItem.withCredentials = false;
return { fileItem, form };
};
// Insert or update an entry in the responses array
const upsertResponse = fileItem => {
// Run the update in a custom zone since for some reason change detection isn't performed
// as part of the XHR request to upload the files.
// Running in a custom zone forces change detection
this.zone.run(() => {
// Update an existing entry if it's upload hasn't completed yet
// Find the id of an existing item
const existingId = this.responses.reduce((prev, current, index) => {
if (current.file.name === fileItem.file.name && !current.status) {
return index;
}
return prev;
}, -1);
if (existingId > -1) {
// Update existing item with new data
this.responses[existingId] = Object.assign(this.responses[existingId], fileItem);
} else {
// Create new response
this.responses.push(fileItem);
}
});
};
this.uploader.onCompleteItem = (item: any, response: string, status: number, headers: ParsedResponseHeaders) =>
upsertResponse(
{
file: item.file,
status,
data: JSON.parse(response)
}
);
this.uploader.onProgressItem = (fileItem: any, progress: any) =>
upsertResponse(
{
file: fileItem.file,
progress
}
);
}
updateTitle(value: string) {
this.title = value;
}
public fileOverBase(e: any): void {
this.hasBaseDropZoneOver = e;
}
getFileProperties(fileProperties: any) {
// Transforms Javascript Object to an iterable to be used by *ngFor
if (!fileProperties) {
return null;
}
return Object.keys(fileProperties)
.map((key) => ({ 'key': key, 'value': fileProperties[key] }));
}
}