AttachmentDataSource.java
3.34 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.api.request.RequestParameter
* org.apache.tika.config.TikaConfig
* org.apache.tika.detect.Detector
* org.apache.tika.io.TikaInputStream
* org.apache.tika.metadata.Metadata
* org.apache.tika.mime.MediaType
* org.apache.tika.mime.MimeType
* org.apache.tika.mime.MimeTypeException
* org.apache.tika.mime.MimeTypes
*/
package com.day.cq.wcm.foundation.forms.attachments;
import com.day.cq.wcm.foundation.forms.attachments.FileDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.sling.api.request.RequestParameter;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
public class AttachmentDataSource
implements FileDataSource {
private final RequestParameter fileRequestParameter;
public AttachmentDataSource(RequestParameter fileRequestParameter) {
this.fileRequestParameter = fileRequestParameter;
}
@Override
public String getContentType() {
try {
TikaConfig config = TikaConfig.getDefaultConfig();
Detector detector = config.getDetector();
TikaInputStream stream = TikaInputStream.get((InputStream)this.getInputStream());
Metadata metadata = new Metadata();
metadata.add("resourceName", this.getName());
MediaType mediaType = detector.detect((InputStream)stream, metadata);
return mediaType.getBaseType().toString();
}
catch (IOException e) {
return this.getName();
}
}
@Override
public String getType() {
try {
TikaConfig config = TikaConfig.getDefaultConfig();
Detector detector = config.getDetector();
TikaInputStream stream = TikaInputStream.get((InputStream)this.getInputStream());
Metadata metadata = new Metadata();
metadata.add("resourceName", this.getName());
MediaType mediaType = detector.detect((InputStream)stream, metadata);
MimeType mimeType = config.getMimeRepository().forName(mediaType.toString());
return mimeType.getExtension();
}
catch (IOException e) {
return this.getName();
}
catch (MimeTypeException e) {
return this.getName();
}
}
@Override
public String getTypeFromFileName() {
String fileName = this.getName();
int index = fileName.lastIndexOf(".");
if (index > -1) {
return fileName.substring(index);
}
return null;
}
@Override
public long getSize() {
return this.fileRequestParameter.getSize();
}
@Override
public InputStream getInputStream() throws IOException {
return this.fileRequestParameter.getInputStream();
}
@Override
public String getName() {
return this.fileRequestParameter.getFileName();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("not implemented");
}
}