Repository navigation
Expand file tree
/
Copy pathFileStorage.java
More file actions
151 lines (129 loc) · 3.68 KB
/
Copy pathFileStorage.java
File metadata and controls
151 lines (129 loc) · 3.68 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
public class FileStorage implements FileStorageInterface {
HashMap<String, FileServer> stored;
public FileStorage() {
stored = new HashMap<String, FileServer>();
// File 1
ArrayList<String> tagsF1 = new ArrayList<String>();
tagsF1.add("Informatique");
tagsF1.add("Anglais");
tagsF1.add("Ordinateur");
FileServer file1 = new FileServer("file1.txt", tagsF1);
stored.put("file1.txt", file1);
// File 2
ArrayList<String> tagsF2 = new ArrayList<String>();
tagsF2.add("Sport");
tagsF2.add("Football");
tagsF2.add("Zidane");
FileServer file2 = new FileServer("file2.txt", tagsF2);
stored.put("file2.txt", file2);
// File 3
ArrayList<String> tagsF3 = new ArrayList<String>();
tagsF3.add("Recette");
tagsF3.add("Cuisine");
tagsF3.add("Tajine");
FileServer file3 = new FileServer("file3.jpg", tagsF3);
stored.put("file3.jpg", file3);
// File 4
ArrayList<String> tagsF4 = new ArrayList<String>();
tagsF4.add("Film");
tagsF4.add("Suspense");
tagsF4.add("James Bond");
FileServer file4 = new FileServer("file4.jpg", tagsF4);
stored.put("file4.jpg", file4);
}
public String get(String fileName) {
if (stored.containsKey(fileName)) {
FileServer s = stored.get(fileName);
StringBuilder sb = new StringBuilder();
sb.append("FIC " + s.getFile().getName() + "\n");
sb.append("TAGS: ");
for (String tag : s.getTags()) {
sb.append(tag);
if (s.getTags().size() - 1 != s.getTags().indexOf(tag)) {
sb.append(", ");
} else {
sb.append("\n");
}
}
sb.append("TYPE: " + s.getType() + "\n");
if (s.getType().equals("txt")) {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(s.getFile()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line+"\n");
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
byte[] bytes;
try {
bytes = loadFile(s.getFile());
byte[] encoded = Base64.getEncoder().encode(bytes);
String encodedString = new String(encoded);
sb.append(encodedString+"\n");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return sb.toString();
} else {
return null;
}
}
public String list() {
StringBuilder sb = new StringBuilder();
sb.append("NUM " + stored.size() + " \n");
for (String fileName : stored.keySet()) {
sb.append(fileName + " \n");
FileServer s = stored.get(fileName);
for (String tag : s.getTags()) {
sb.append(tag);
if (s.getTags().size() - 1 != s.getTags().indexOf(tag)) {
sb.append(" , ");
} else {
sb.append("\n");
}
}
}
return sb.toString();
}
private static byte[] loadFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
is.close();
return bytes;
}
}