FileDescriptor#getFileName возвращает не то, что ожидается. #PL-1997

This commit is contained in:
Konstantin Krivopustov 2013-05-30 11:47:03 +00:00
parent 64993605c9
commit 882133e5a4
7 changed files with 48 additions and 72 deletions

View File

@ -85,11 +85,11 @@ public class FileStorage implements FileStorageAPI {
if (roots.length == 0) {
log.error("No storage directories defined");
throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, fileDescr.getFileName());
throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, fileDescr.getId().toString());
}
if (!roots[0].exists()) {
log.error("Inaccessible primary storage at " + roots[0]);
throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, fileDescr.getFileName());
throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, fileDescr.getId().toString());
}
File dir = getStorageDir(roots[0], fileDescr.getCreateDate());
@ -97,7 +97,7 @@ public class FileStorage implements FileStorageAPI {
if (!dir.exists())
throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, dir.getAbsolutePath());
final File file = new File(dir, fileDescr.getFileName());
final File file = new File(dir, getFileName(fileDescr));
if (file.exists())
throw new FileStorageException(FileStorageException.Type.FILE_ALREADY_EXISTS, file.getAbsolutePath());
@ -123,7 +123,7 @@ public class FileStorage implements FileStorageAPI {
}
File copyDir = getStorageDir(roots[i], fileDescr.getCreateDate());
final File fileCopy = new File(copyDir, fileDescr.getFileName());
final File fileCopy = new File(copyDir, getFileName(fileDescr));
writeExecutor.submit(new Runnable() {
@Override
@ -186,7 +186,7 @@ public class FileStorage implements FileStorageAPI {
for (File root : roots) {
File dir = getStorageDir(root, fileDescr.getCreateDate());
File file = new File(dir, fileDescr.getFileName());
File file = new File(dir, getFileName(fileDescr));
if (file.exists()) {
if (!file.delete())
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Unable to delete file " + file.getAbsolutePath());
@ -204,14 +204,14 @@ public class FileStorage implements FileStorageAPI {
File[] roots = getStorageRoots();
if (roots.length == 0) {
log.error("No storage directories available");
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getFileName());
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getId().toString());
}
InputStream inputStream = null;
for (File root : roots) {
File dir = getStorageDir(root, fileDescr.getCreateDate());
File file = new File(dir, fileDescr.getFileName());
File file = new File(dir, getFileName(fileDescr));
if (!file.exists()) {
log.error("File " + file + " not found");
continue;
@ -227,7 +227,7 @@ public class FileStorage implements FileStorageAPI {
if (inputStream != null)
return inputStream;
else
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getFileName());
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getId().toString());
}
@Override
@ -236,7 +236,7 @@ public class FileStorage implements FileStorageAPI {
try {
return IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getFileName(), e);
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getId().toString(), e);
}
}
@ -268,4 +268,8 @@ public class FileStorage implements FileStorageAPI {
+ StringUtils.leftPad(String.valueOf(month), 2, '0') + "/"
+ StringUtils.leftPad(String.valueOf(day), 2, '0'));
}
public static String getFileName(FileDescriptor fileDescriptor) {
return fileDescriptor.getId().toString() + "." + fileDescriptor.getExtension();
}
}

View File

@ -55,7 +55,7 @@ public class FileStorage implements FileStorageMBean {
List<FileDescriptor> fileDescriptors = query.getResultList();
for (FileDescriptor fileDescriptor : fileDescriptors) {
File dir = fileStorage.getStorageDir(roots[0], fileDescriptor.getCreateDate());
File file = new File(dir, fileDescriptor.getFileName());
File file = new File(dir, com.haulmont.cuba.core.app.FileStorage.getFileName(fileDescriptor));
if (!file.exists()) {
sb.append(fileDescriptor.getId())
.append(", ")
@ -107,7 +107,7 @@ public class FileStorage implements FileStorageMBean {
Set<String> descriptorsFileNames = new HashSet<>();
for (FileDescriptor fileDescriptor : fileDescriptors) {
descriptorsFileNames.add(fileDescriptor.getFileName());
descriptorsFileNames.add(com.haulmont.cuba.core.app.FileStorage.getFileName(fileDescriptor));
}
for (File file : systemFiles) {

View File

@ -12,8 +12,6 @@ import org.apache.commons.lang.text.StrBuilder;
import javax.persistence.Column;
import javax.persistence.Table;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@ -29,8 +27,6 @@ public class FileDescriptor extends StandardEntity {
private static final long serialVersionUID = 564683944299730504L;
public static final String DATE_FMT = "yyyy-MM-dd";
@Column(name = "NAME", length = 500)
private String name;
@ -43,6 +39,9 @@ public class FileDescriptor extends StandardEntity {
@Column(name = "CREATE_DATE")
private Date createDate;
/**
* @return file uploading timestamp
*/
public Date getCreateDate() {
return createDate;
}
@ -51,6 +50,9 @@ public class FileDescriptor extends StandardEntity {
this.createDate = createDate;
}
/**
* @return file name including extension
*/
public String getName() {
return name;
}
@ -59,14 +61,20 @@ public class FileDescriptor extends StandardEntity {
this.name = name;
}
/**
* @return file extension, i.e. the part of name after the last dot
*/
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
this.extension = StringUtils.substring(extension, 0, 20);
}
/**
* @return file size in bytes
*/
public Integer getSize() {
return size;
}
@ -75,20 +83,9 @@ public class FileDescriptor extends StandardEntity {
this.size = size;
}
public String getFileExt() {
if (name != null) {
int i = name.lastIndexOf('.');
if (i > -1) {
return StringUtils.substring(name, i + 1).toLowerCase();
}
}
return "";
}
public String getFileName() {
return id.toString() + "." + getExtension();
}
/**
* Used by the framework to transfer file between application tiers.
*/
public String toUrlParam() {
return new StrBuilder()
.append(id).append(",")
@ -97,6 +94,9 @@ public class FileDescriptor extends StandardEntity {
.toString();
}
/**
* Used by the framework to transfer file between application tiers.
*/
public static FileDescriptor fromUrlParam(String urlParam) {
String[] parts = urlParam.split(",");
if (parts.length != 3)

View File

@ -11,6 +11,7 @@ import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.upload.FileUploadingAPI;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
@ -54,14 +55,6 @@ public class FileEditor extends AbstractEditor<FileDescriptor> {
public void init(Map<String, Object> params) {
}
private String getFileExt(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > -1)
return StringUtils.substring(fileName, i + 1, i + 20);
else
return "";
}
@Override
public void setItem(Entity item) {
super.setItem(item);
@ -98,7 +91,7 @@ public class FileEditor extends AbstractEditor<FileDescriptor> {
public void uploadSucceeded(Event event) {
getItem().setName(uploadField.getFileName());
getItem().setCreateDate(timeSource.currentTimestamp());
getItem().setExtension(getFileExt(uploadField.getFileName()));
getItem().setExtension(FilenameUtils.getExtension(uploadField.getFileName()));
FileUploadingAPI fileUploading = AppBeans.get(FileUploadingAPI.NAME);
File file = fileUploading.getFile(uploadField.getFileId());

View File

@ -10,6 +10,7 @@ import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.core.sys.remoting.ClusterInvocationSupport;
import com.haulmont.cuba.gui.executors.TaskLifeCycle;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -186,18 +187,13 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
FileDescriptor fDesc = new FileDescriptor();
fDesc.setSize(fileSize);
fDesc.setExtension(getFileExt(name));
fDesc.setExtension(FilenameUtils.getExtension(name));
fDesc.setName(name);
fDesc.setCreateDate(timeSource.currentTimestamp());
return fDesc;
}
private String getFileExt(String fileName) {
int i = fileName.lastIndexOf('.');
return i > -1 ? StringUtils.substring(fileName, i + 1, i + 20) : "";
}
@Override
public InputStream loadFile(UUID fileId) throws FileNotFoundException {
File f = tempFiles.get(fileId);
@ -239,7 +235,7 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
try {
uploadFileIntoStorage(fileId, fileDescr, null);
} catch (InterruptedIOException e) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getFileName());
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getId().toString());
}
deleteFile(fileId);

View File

@ -5,20 +5,13 @@
*/
package com.haulmont.cuba.web.app;
import com.haulmont.chile.core.model.Instance;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.web.filestorage.WebExportDisplay;
import com.haulmont.cuba.web.gui.components.WebComponentsHelper;
import com.vaadin.data.Property;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import org.apache.commons.lang.StringUtils;
import java.text.NumberFormat;
@ -28,20 +21,6 @@ import java.text.NumberFormat;
*/
public class FileDownloadHelper {
/**
* @deprecated Use {@link org.apache.commons.io.FilenameUtils#getExtension(String)}
* @param fileName
* @return
*/
@Deprecated
public static String getFileExt(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > -1)
return StringUtils.substring(fileName, i + 1, i + 20);
else
return "";
}
public static String makeLink(FileDescriptor fd, boolean newWindow, boolean attachment) {
StringBuilder sb = new StringBuilder();
sb.append("<a href=\"");

View File

@ -10,12 +10,16 @@ import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.global.TimeProvider;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.AbstractWindow;
import com.haulmont.cuba.gui.components.Button;
import com.haulmont.cuba.gui.components.FileUploadField;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.components.actions.RemoveAction;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.upload.FileUploadingAPI;
import com.haulmont.cuba.web.app.FileDownloadHelper;
import org.apache.commons.io.FilenameUtils;
import java.io.File;
import java.util.Map;
@ -60,7 +64,7 @@ public class FileFrameController extends AbstractWindow {
public void uploadSucceeded(Event event) {
fd = new FileDescriptor();
fd.setName(uploadField.getFileName());
fd.setExtension(FileDownloadHelper.getFileExt(uploadField.getFileName()));
fd.setExtension(FilenameUtils.getExtension(uploadField.getFileName()));
FileUploadingAPI fileUploading = AppBeans.get(FileUploadingAPI.NAME);
File file = fileUploading.getFile(uploadField.getFileId());