19 package org.turro.upload;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.Reader;
26 import java.nio.charset.Charset;
27 import java.util.function.Consumer;
28 import java.util.function.Function;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import org.turro.formatter.BytesFormatter;
32 import org.apache.commons.io.input.ReaderInputStream;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.util.Toasts;
35 import org.turro.file.Document;
36 import org.turro.file.Folder;
37 import org.turro.i18n.I_;
38 import org.turro.util.ImageUtil;
39 import org.turro.zkoss.text.RepositoryContent;
40 import org.zkoss.util.media.Media;
48 public static final long MAX_FILE_SIZE = BytesFormatter.parseBytes(
"16MB");
50 public static void toFolder(Media media, Folder folder) {
54 public static void toFolder(Media media, Folder folder, Consumer<File> consumer) {
55 toFolder(media, folder.folder(), consumer);
58 public static void toFolder(Media media, File folder) {
62 public static void toFolder(Media media, File folder, Consumer<File> consumer) {
63 toFile(media, Folder.from(folder).ensure().path().resolve(Document.correctName(media.getName())).toFile(), consumer);
66 public static void toFile(Media media, File file) {
70 public static void toFile(Media media, File file, Consumer<File> consumer) {
71 try(FileOutputStream fos =
new FileOutputStream(file)) {
72 if(media.inMemory()) {
73 fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
75 byte[] buffer =
new byte[102400];
76 try(InputStream is =
toStream(media)) {
78 while((r = is.read(buffer)) != -1) {
79 fos.write(buffer, 0, r);
83 }
catch (IOException ex) {
86 if(file.exists() && consumer !=
null) {
87 consumer.accept(file);
92 if(!media.isBinary()) {
93 return media.getReaderData();
98 public static InputStream
toStream(Media media) {
99 return media.isBinary() ? media.getStreamData() :
new ReaderInputStream(media.getReaderData(), Charset.defaultCharset());
102 public static Consumer<File>
scaling(
double scale) {
105 ImageUtil.scale(file, file, scale);
106 }
catch (IOException ex) {
114 public static Consumer<File>
smart(
double width,
double height) {
117 ImageUtil.smart(file, file, width, height);
118 }
catch (IOException ex) {
128 if(Document.from(file).isImage()) {
130 ImageUtil.scale(file, file, 1200);
131 }
catch (IOException ex) {
142 public static Function<File, Boolean>
smaller(
long size) {
144 if(file.length() > size) {
static String logMsg(String msg)
static Toasts message(String message)
static String get(String msg)