-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInfo.java
More file actions
216 lines (191 loc) · 5.59 KB
/
FileInfo.java
File metadata and controls
216 lines (191 loc) · 5.59 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
/*-
* #%L
* Upload Helper Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.flowingcode.vaadin.addons.uploadhelper;
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
import com.vaadin.flow.component.upload.FinishedEvent;
import com.vaadin.flow.component.upload.Upload;
import elemental.json.Json;
import elemental.json.JsonObject;
import java.io.Serializable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.experimental.ExtensionMethod;
/** Represents a file in the file list of an {@link Upload} component. */
@SuppressWarnings("serial")
@ExtensionMethod(value = JsonMigration.class, suppressBaseMethods = true)
public final class FileInfo implements Serializable {
private final String name;
@Getter(AccessLevel.PACKAGE)
private Upload upload;
private Boolean complete;
private Boolean indeterminate;
private String errorMessage;
private Integer progress;
private String status;
/**
* Constructs a {@code FileInfo} object with the specified upload and file name.
*
* @param upload the {@code Upload} instance representing the file upload source
* @param name the name of the file being uploaded
*/
public FileInfo(Upload upload, String name) {
this.upload = upload;
this.name = name;
}
/**
* Constructs a {@code FileInfo} object from the specified finished upload event.
*
* @param ev the {@code FinishedEvent} representing the succeeded or failed event
*/
public FileInfo(FinishedEvent ev) {
upload = ev.getSource();
name = ev.getFileName();
}
/** Updates this file in the upload component. */
public void update() {
update(false);
}
/** Adds a new file to the upload component. */
public void create() {
update(true);
}
private void update(boolean createIfNotExists) {
upload.getElement().executeJs(
"""
var d = this;
var i = d.files.findIndex(f=>f.name==$0.name);
if (i<0) {
if ($1) d.files = [... d.files, $0]; else return;
} else {
if (d.files.some((e,j)=>e.name==$0.name && j>i)) d.files=d.files.filter((e,j)=>e.name!=$0.name || j<=i);
delete $0.name;
d.files[i] = Object.assign(d.files[i], $0);
}
d.files = Array.from(d.files);
""",
toJson(), createIfNotExists);
}
private JsonObject toJson() {
JsonObject json = Json.createObject();
json.put("name", name);
if (complete != null) {
json.put("complete", complete);
}
if (indeterminate != null) {
json.put("indeterminate", indeterminate);
}
if (errorMessage != null) {
json.put("error", errorMessage);
}
if (progress != null) {
json.put("progress", progress);
}
if (status != null) {
json.put("status", status);
}
return json;
}
/**
* True if uploading is completed, false otherwise.
*
* @return This instance for method chaining
*/
public FileInfo complete(Boolean complete) {
this.complete = complete;
return this;
}
/**
* Configure the upload in complete state (i.e. uploading is completed)
*
* @return This instance for method chaining
*/
public FileInfo complete() {
complete = true;
return this;
}
/**
* Configure the upload in indeterminate state (i.e. the remaining time is unknown)
*
* @return This instance for method chaining
*/
public FileInfo indeterminate() {
return indeterminate(true);
}
/**
* True if the remaining time is unknown, false otherwise.
*
* @return This instance for method chaining
*/
public FileInfo indeterminate(Boolean indeterminate) {
this.indeterminate = indeterminate;
return this;
}
/**
* Error message returned by the server, if any.
*
* @return This instance for method chaining
*/
public FileInfo errorMessage(String errorMessage) {
this.errorMessage = errorMessage;
return this;
}
/**
* Number representing the uploading progress.
*
* @return This instance for method chaining
*/
public FileInfo progress(Integer progress) {
this.progress = progress;
return this;
}
/**
* Uploading status message.
*
* @return This instance for method chaining
*/
public FileInfo status(String status) {
this.status = status;
return this;
}
/** Returns the name of the uploaded file. */
public String getName() {
return name;
}
/** Returns {@code true} if uploading is completed, false otherwise. */
public Boolean getComplete() {
return complete;
}
/** Returns {@code true} if the remaining progress is unknown, false otherwise. */
public Boolean getIndeterminate() {
return indeterminate;
}
/** Returns the error message returned by the server. */
public String getErrorMessage() {
return errorMessage;
}
/** Returns a number between 0 and 100, representing the uploading progress. */
public Integer getProgress() {
return progress;
}
/** Returns the uploading status. */
public String getStatus() {
return status;
}
}