1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-15 02:36:24 +00:00

Closes #119: Keep and display original file name

This commit is contained in:
jendib
2016-12-07 01:28:52 +01:00
parent 4f7fcbfdf0
commit bb3faca533
13 changed files with 94 additions and 34 deletions

View File

@@ -10,6 +10,8 @@ import javax.persistence.Table;
import javax.persistence.Transient;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.sismics.util.mime.MimeTypeUtil;
/**
* File entity.
@@ -38,12 +40,18 @@ public class File implements Loggable {
@Column(name = "FIL_IDUSER_C", length = 36, nullable = false)
private String userId;
/**
* Name.
*/
@Column(name = "FIL_NAME_C", length = 200)
private String name;
/**
* MIME type.
*/
@Column(name = "FIL_MIMETYPE_C", length = 100)
private String mimeType;
/**
* OCR-ized content.
*/
@@ -92,6 +100,15 @@ public class File implements Loggable {
this.documentId = documentId;
}
public String getName() {
return name;
}
public File setName(String name) {
this.name = name;
return this;
}
public String getMimeType() {
return mimeType;
}
@@ -153,6 +170,7 @@ public class File implements Loggable {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("name", name)
.toString();
}
@@ -160,4 +178,17 @@ public class File implements Loggable {
public String toMessage() {
return documentId;
}
/**
* Build the full file name.
*
* @param def Default name if the file doesn't have one.
* @return File name
*/
public String getFullName(String def) {
if (Strings.isNullOrEmpty(name)) {
return def + "." + MimeTypeUtil.getFileExtension(mimeType);
}
return name;
}
}

View File

@@ -20,5 +20,9 @@ public class MimeType {
public static final String OFFICE_DOCUMENT = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
public static final String TEXT_PLAIN = "text/plain";
public static final String TEXT_CSV = "text/csv";
public static final String DEFAULT = "application/octet-stream";
}

View File

@@ -21,32 +21,30 @@ public class MimeTypeUtil {
* Try to guess the MIME type of a file by its magic number (header).
*
* @param is Stream to inspect
* @param name File name
* @return MIME type
* @throws IOException
* @throws IOException e
*/
public static String guessMimeType(InputStream is) throws IOException {
public static String guessMimeType(InputStream is, String name) throws IOException {
byte[] headerBytes = new byte[64];
is.mark(headerBytes.length);
int readCount = is.read(headerBytes);
is.read(headerBytes);
is.reset();
if (readCount <= 0) {
throw new IOException("Cannot read input file");
}
return guessMimeType(headerBytes);
return guessMimeType(headerBytes, name);
}
/**
* Try to guess the MIME type of a file by its magic number (header).
*
* @param headerBytes File header (first bytes)
* @param name File name
* @return MIME type
* @throws UnsupportedEncodingException
* @throws UnsupportedEncodingException e
*/
public static String guessMimeType(byte[] headerBytes) throws UnsupportedEncodingException {
public static String guessMimeType(byte[] headerBytes, String name) throws UnsupportedEncodingException {
String header = new String(headerBytes, "US-ASCII");
// Detect by header bytes
if (header.startsWith("PK")) {
return MimeType.APPLICATION_ZIP;
} else if (header.startsWith("GIF87a") || header.startsWith("GIF89a")) {
@@ -59,7 +57,16 @@ public class MimeTypeUtil {
} else if (headerBytes[0] == ((byte) 0x25) && headerBytes[1] == ((byte) 0x50) && headerBytes[2] == ((byte) 0x44) && headerBytes[3] == ((byte) 0x46)) {
return MimeType.APPLICATION_PDF;
}
// Detect by file extension
if (name != null) {
if (name.endsWith(".txt")) {
return MimeType.TEXT_PLAIN;
} else if (name.endsWith(".csv")) {
return MimeType.TEXT_CSV;
}
}
return MimeType.DEFAULT;
}
@@ -86,7 +93,7 @@ public class MimeTypeUtil {
case MimeType.OFFICE_DOCUMENT:
return "docx";
default:
return null;
return "bin";
}
}

View File

@@ -1 +1 @@
db.version=10
db.version=11

View File

@@ -0,0 +1,2 @@
alter table T_FILE add column FIL_NAME_C varchar(200);
update T_CONFIG set CFG_VALUE_C = '11' where CFG_ID_C = 'DB_VERSION';