1 package media;
2
3
4 import java.io.*;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.UUID;
8
9
10
11
12 public class MediaRepository implements Serializable{
13
14 private Path uploadRoot = Path.of(System.getProperty("openejb.home"), "uploads");
15 private final long sizeLimit = 5*1024*1024;
16
17
18
19
20
21 public MediaRepository(){}
22
23
24
25
26
27
28 public MediaRepository(Path uploadRoot){
29 this.uploadRoot = uploadRoot;
30 }
31
32
33
34
35
36
37
38
39
40 public String insert(InputStream stream, String filename) throws IOException {
41 Files.createDirectories(uploadRoot);
42 File file = new File(uploadRoot.toFile(), filename);
43 Files.copy(new LimitedInputStream(stream,sizeLimit), file.toPath());
44 return filename;
45 }
46
47
48
49
50
51
52
53 public String insert(InputStream stream) throws IOException {
54 return insert(stream, UUID.randomUUID().toString());
55 }
56
57
58
59
60
61
62 public void remove(String filename) throws IOException {
63 boolean successful = Files.deleteIfExists(uploadRoot.resolve(filename));
64
65 }
66
67
68
69
70
71
72 public InputStream get(String filename){
73 File file = new File(uploadRoot.toFile(), filename);
74 if (!file.exists() || !file.isFile())
75 return null;
76 try {
77 return new FileInputStream(file);
78 } catch (FileNotFoundException e) {
79 return null;
80 }
81 }
82
83 }