|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Upload Helper Add-on |
| 4 | + * %% |
| 5 | + * Copyright (C) 2022 - 2024 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.uploadhelper; |
| 21 | + |
| 22 | +import com.vaadin.flow.component.upload.FinishedEvent; |
| 23 | +import com.vaadin.flow.component.upload.Upload; |
| 24 | +import elemental.json.Json; |
| 25 | +import elemental.json.JsonObject; |
| 26 | +import java.io.Serializable; |
| 27 | +import lombok.AccessLevel; |
| 28 | +import lombok.Getter; |
| 29 | + |
| 30 | +/** Represents a file in the file list of an {@link Upload} component. */ |
| 31 | +@SuppressWarnings("serial") |
| 32 | +public final class FileInfo implements Serializable { |
| 33 | + |
| 34 | + private final String name; |
| 35 | + |
| 36 | + @Getter(AccessLevel.PACKAGE) |
| 37 | + private Upload upload; |
| 38 | + |
| 39 | + private Boolean complete; |
| 40 | + private Boolean indeterminate; |
| 41 | + private String errorMessage; |
| 42 | + private Integer progress; |
| 43 | + private String status; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a {@code FileInfo} object with the specified upload and file name. |
| 47 | + * |
| 48 | + * @param upload the {@code Upload} instance representing the file upload source |
| 49 | + * @param name the name of the file being uploaded |
| 50 | + */ |
| 51 | + public FileInfo(Upload upload, String name) { |
| 52 | + this.upload = upload; |
| 53 | + this.name = name; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs a {@code FileInfo} object from the specified finished upload event. |
| 58 | + * |
| 59 | + * @param ev the {@code FinishedEvent} representing the succeeded or failed event |
| 60 | + */ |
| 61 | + public FileInfo(FinishedEvent ev) { |
| 62 | + upload = ev.getSource(); |
| 63 | + name = ev.getFileName(); |
| 64 | + } |
| 65 | + |
| 66 | + /** Updates this file in the upload component. */ |
| 67 | + public void update() { |
| 68 | + update(false); |
| 69 | + } |
| 70 | + |
| 71 | + /** Adds a new file to the upload component. */ |
| 72 | + public void create() { |
| 73 | + update(true); |
| 74 | + } |
| 75 | + |
| 76 | + private void update(boolean createIfNotExists) { |
| 77 | + upload.getElement().executeJs( |
| 78 | + """ |
| 79 | + var d = this; |
| 80 | + var i = d.files.findIndex(f=>f.name==$0.name); |
| 81 | + if (i<0) { |
| 82 | + if ($1) d.files = [... d.files, $0]; else return; |
| 83 | + } else { |
| 84 | + 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); |
| 85 | + d.files[i] = Object.assign(d.files[i], $0); |
| 86 | + } |
| 87 | + d.files = Array.from(d.files); |
| 88 | + """, |
| 89 | + toJson(), createIfNotExists); |
| 90 | + } |
| 91 | + |
| 92 | + private JsonObject toJson() { |
| 93 | + JsonObject json = Json.createObject(); |
| 94 | + json.put("name", name); |
| 95 | + if (complete != null) { |
| 96 | + json.put("complete", complete); |
| 97 | + } |
| 98 | + if (indeterminate != null) { |
| 99 | + json.put("indeterminate", indeterminate); |
| 100 | + } |
| 101 | + if (errorMessage != null) { |
| 102 | + json.put("error", errorMessage); |
| 103 | + } |
| 104 | + if (progress != null) { |
| 105 | + json.put("progress", progress); |
| 106 | + } |
| 107 | + if (status != null) { |
| 108 | + json.put("status", status); |
| 109 | + } |
| 110 | + return json; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * True if uploading is completed, false otherwise. |
| 115 | + * |
| 116 | + * @return This instance for method chaining |
| 117 | + */ |
| 118 | + public FileInfo complete(Boolean complete) { |
| 119 | + this.complete = complete; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Configure the upload in complete state (i.e. uploading is completed) |
| 125 | + * |
| 126 | + * @return This instance for method chaining |
| 127 | + */ |
| 128 | + public FileInfo complete() { |
| 129 | + complete = true; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Configure the upload in indeterminate state (i.e. the remaining time is unknown) |
| 135 | + * |
| 136 | + * @return This instance for method chaining |
| 137 | + */ |
| 138 | + public FileInfo indeterminate() { |
| 139 | + return indeterminate(true); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * True if the remaining time is unknown, false otherwise. |
| 144 | + * |
| 145 | + * @return This instance for method chaining |
| 146 | + */ |
| 147 | + public FileInfo indeterminate(Boolean indeterminate) { |
| 148 | + this.indeterminate = indeterminate; |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Error message returned by the server, if any. |
| 154 | + * |
| 155 | + * @return This instance for method chaining |
| 156 | + */ |
| 157 | + public FileInfo errorMessage(String errorMessage) { |
| 158 | + this.errorMessage = errorMessage; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Number representing the uploading progress. |
| 164 | + * |
| 165 | + * @return This instance for method chaining |
| 166 | + */ |
| 167 | + public FileInfo progress(Integer progress) { |
| 168 | + this.progress = progress; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Uploading status message. |
| 174 | + * |
| 175 | + * @return This instance for method chaining |
| 176 | + */ |
| 177 | + public FileInfo status(String status) { |
| 178 | + this.status = status; |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + /** Returns the name of the uploaded file. */ |
| 183 | + public String getName() { |
| 184 | + return name; |
| 185 | + } |
| 186 | + |
| 187 | + /** Returns {@code true} if uploading is completed, false otherwise. */ |
| 188 | + public Boolean getComplete() { |
| 189 | + return complete; |
| 190 | + } |
| 191 | + |
| 192 | + /** Returns {@code true} if the remaining progress is unknown, false otherwise. */ |
| 193 | + public Boolean getIndeterminate() { |
| 194 | + return indeterminate; |
| 195 | + } |
| 196 | + |
| 197 | + /** Returns the error message returned by the server. */ |
| 198 | + public String getErrorMessage() { |
| 199 | + return errorMessage; |
| 200 | + } |
| 201 | + |
| 202 | + /** Returns a number between 0 and 100, representing the uploading progress. */ |
| 203 | + public Integer getProgress() { |
| 204 | + return progress; |
| 205 | + } |
| 206 | + |
| 207 | + /** Returns the uploading status. */ |
| 208 | + public String getStatus() { |
| 209 | + return status; |
| 210 | + } |
| 211 | + |
| 212 | +} |
0 commit comments