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
|
/*******************************************************************************
* Copyright (c) 2004, 2008 Tasktop Technologies and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tasktop Technologies - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.tasks.core.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentSource;
/**
* @author Steffen Pingel
*/
public class FileTaskAttachmentSource extends AbstractTaskAttachmentSource {
public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
private static Map<String, String> extensions2Types;
static {
extensions2Types = new HashMap<String, String>();
extensions2Types.put("txt", "text/plain");
extensions2Types.put("html", "text/html");
extensions2Types.put("htm", "text/html");
extensions2Types.put("jpg", "image/jpeg");
extensions2Types.put("jpeg", "image/jpeg");
extensions2Types.put("gif", "image/gif");
extensions2Types.put("png", "image/png");
extensions2Types.put("xml", "application/xml");
extensions2Types.put("zip", APPLICATION_OCTET_STREAM);
extensions2Types.put("tar", APPLICATION_OCTET_STREAM);
extensions2Types.put("gz", APPLICATION_OCTET_STREAM);
}
public static String getContentTypeFromFilename(String fileName) {
int index = fileName.lastIndexOf(".");
if (index > 0 && index < fileName.length()) {
String ext = fileName.substring(index + 1);
String type = extensions2Types.get(ext.toLowerCase(Locale.ENGLISH));
if (type != null) {
return type;
}
}
return APPLICATION_OCTET_STREAM;
}
private String contentType;
private String description;
private final File file;
private String name;
public FileTaskAttachmentSource(File file) {
this.file = file;
this.name = file.getName();
this.contentType = getContentTypeFromFilename(name);
}
@Override
public InputStream createInputStream(IProgressMonitor monitor) throws CoreException {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, e.getMessage(), e));
}
}
@Override
public String getContentType() {
return contentType;
}
@Override
public String getDescription() {
return description;
}
@Override
public long getLength() {
return file.length();
}
@Override
public String getName() {
return name;
}
@Override
public boolean isLocal() {
return true;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setDescription(String description) {
this.description = description;
}
public void setName(String name) {
this.name = name;
}
}
|