BrightSide Workbench Full Report + Source Code
Medias.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.upload;
20 
21 import java.io.File;
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;
41 
46 public class Medias {
47 
48  public static final long MAX_FILE_SIZE = BytesFormatter.parseBytes("16MB");
49 
50  public static void toFolder(Media media, Folder folder) {
51  toFolder(media, folder, standard());
52  }
53 
54  public static void toFolder(Media media, Folder folder, Consumer<File> consumer) {
55  toFolder(media, folder.folder(), consumer);
56  }
57 
58  public static void toFolder(Media media, File folder) {
59  toFolder(media, folder, standard());
60  }
61 
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);
64  }
65 
66  public static void toFile(Media media, File file) {
67  toFile(media, file, standard());
68  }
69 
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());
74  } else {
75  byte[] buffer = new byte[102400];
76  try(InputStream is = toStream(media)) {
77  int r;
78  while((r = is.read(buffer)) != -1) {
79  fos.write(buffer, 0, r);
80  }
81  }
82  }
83  } catch (IOException ex) {
84  Logger.getLogger(RepositoryContent.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
85  }
86  if(file.exists() && consumer != null) {
87  consumer.accept(file);
88  }
89  }
90 
91  public static Reader toReader(Media media) {
92  if(!media.isBinary()) {
93  return media.getReaderData();
94  }
95  return null;
96  }
97 
98  public static InputStream toStream(Media media) {
99  return media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData(), Charset.defaultCharset());
100  }
101 
102  public static Consumer<File> scaling(double scale) {
103  return (file) -> {
104  try {
105  ImageUtil.scale(file, file, scale);
106  } catch (IOException ex) {
107  Toasts.message(I_.get("Bad format image")).show();
108  file.delete();
109  Logger.getLogger(Medias.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
110  }
111  };
112  }
113 
114  public static Consumer<File> smart(double width, double height) {
115  return (file) -> {
116  try {
117  ImageUtil.smart(file, file, width, height);
118  } catch (IOException ex) {
119  Toasts.message(I_.get("Bad format image")).show();
120  file.delete();
121  Logger.getLogger(Medias.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
122  }
123  };
124  }
125 
126  public static Consumer<File> standard() {
127  return (file) -> {
128  if(Document.from(file).isImage()) {
129  try {
130  ImageUtil.scale(file, file, 1200);
131  } catch (IOException ex) {
132  Toasts.message(I_.get("Bad format image")).show();
133  file.delete();
134  Logger.getLogger(Medias.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
135  }
136  } else {
137  smaller(MAX_FILE_SIZE).apply(file);
138  }
139  };
140  }
141 
142  public static Function<File, Boolean> smaller(long size) {
143  return (file) -> {
144  if(file.length() > size) {
145  Toasts.message(I_.get("File is too large")).show();
146  file.delete();
147  return false;
148  }
149  return true;
150  };
151  }
152 
153  private Medias() {}
154 
155 }
static Toasts message(String message)
Definition: Toasts.java:114
static String get(String msg)
Definition: I_.java:41
static void toFile(Media media, File file, Consumer< File > consumer)
Definition: Medias.java:70
static final long MAX_FILE_SIZE
Definition: Medias.java:48
static void toFile(Media media, File file)
Definition: Medias.java:66
static Consumer< File > smart(double width, double height)
Definition: Medias.java:114
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50
static Consumer< File > standard()
Definition: Medias.java:126
static Consumer< File > scaling(double scale)
Definition: Medias.java:102
static void toFolder(Media media, File folder)
Definition: Medias.java:58
static InputStream toStream(Media media)
Definition: Medias.java:98
static Function< File, Boolean > smaller(long size)
Definition: Medias.java:142
static Reader toReader(Media media)
Definition: Medias.java:91
static void toFolder(Media media, File folder, Consumer< File > consumer)
Definition: Medias.java:62
static void toFolder(Media media, Folder folder, Consumer< File > consumer)
Definition: Medias.java:54