// satisfied. Doing this for folders would require recursive checking
// for all of their children, which in turn would defy the purpose of
// the optimization.
- if (folder == null)
+ if (folder == null){
// Checking If headers.
if (!checkIfHeaders(req, resp, file, oldBody))
return;
+ }
+ else if(!checkIfModifiedSince(req, resp, folder))
+ return;
// Find content type.
String contentType = null;
try {
if(file != null)
if (needsContentDisposition(req))
- resp.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+URLEncoder.encode(file.getName(),"UTF-8"));
+ resp.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+getDispositionFilename(file));
else
- resp.setHeader("Content-Disposition","inline; filename*=UTF-8''"+URLEncoder.encode(file.getName(),"UTF-8"));
+ resp.setHeader("Content-Disposition","inline; filename*=UTF-8''"+getDispositionFilename(file));
if (ostream != null)
copy(file, renderResult, ostream, req, oldBody);
else
}
/**
+ * Return the filename of the specified file properly formatted for
+ * including in the Content-Disposition header.
+ */
+ private String getDispositionFilename(FileHeaderDTO file) throws UnsupportedEncodingException {
+ return URLEncoder.encode(file.getName(),"UTF-8").replaceAll("\\+", "%20");
+ }
+
+ /**
* Determines whether the user agent needs the Content-Disposition
* header to be set, in order to properly download a file.
*
}
}
- private String getBackupFilename(FileHeaderDTO file, String filename){
- List<FileHeaderDTO> deletedFiles = new ArrayList<FileHeaderDTO>();
- try{
- deletedFiles = getService().getDeletedFiles(file.getOwner().getId());
- }
- catch(ObjectNotFoundException e){
-
- } catch (RpcException e) {
-
- }
- List<FileHeaderDTO> filesInSameFolder = new ArrayList<FileHeaderDTO>();
- for(FileHeaderDTO deleted : deletedFiles)
- if(deleted.getFolder().getId().equals(file.getFolder().getId()))
- filesInSameFolder.add(deleted);
- int i=1;
- String filenameToCheck = filename;
- for(FileHeaderDTO same : filesInSameFolder)
- if(same.getName().startsWith(filename)){
- String toCheck=same.getName().substring(filename.length(),same.getName().length());
- if(toCheck.startsWith(" ")){
- int test =-1;
- try{
- test = Integer.valueOf(toCheck.replace(" ",""));
- }
- catch(NumberFormatException e){
- //do nothing since string is not a number
- }
- if(test>=i)
- i = test+1;
- }
- }
-
- return filename+" "+i;
- }
-
/**
* Move the resource in the specified path to the specified destination.
*
if (resource instanceof FolderDTO) {
final FolderDTO folder = (FolderDTO) resource;
String name = json.optString("name");
- if (!name.isEmpty()){
+ if (!name.isEmpty())
try {
name = URLDecoder.decode(name, "UTF-8");
} catch (IllegalArgumentException e) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
return;
}
- final String fName = name;
+ JSONArray permissions = json.optJSONArray("permissions");
+ Set<PermissionDTO> perms = null;
+ if (permissions != null)
+ perms = parsePermissions(user, permissions);
+ if (!name.isEmpty() || permissions != null) {
+ final String fName = name.isEmpty()? null: name;
+ final Set<PermissionDTO> fPerms = perms;
FolderDTO folderUpdated = new TransactionHelper<FolderDTO>().tryExecute(new Callable<FolderDTO>() {
@Override
public FolderDTO call() throws Exception {
- return getService().modifyFolder(user.getId(), folder.getId(), fName);
- }
-
- });
- String parentUrl =URLDecoder.decode(getContextPath(req, true),"UTF-8");
- String fpath = URLDecoder.decode(req.getPathInfo(), "UTF-8");
- parentUrl = parentUrl.replaceAll(fpath, "");
- if(!parentUrl.endsWith("/"))
- parentUrl = parentUrl+"/";
- parentUrl = parentUrl+folderUpdated.getOwner().getUsername()+PATH_FILES+folderUpdated.getPath();
- resp.getWriter().println(parentUrl);
- }
-
- JSONArray permissions = json.optJSONArray("permissions");
- if (permissions != null) {
- final Set<PermissionDTO> perms = parsePermissions(user, permissions);
- new TransactionHelper<Object>().tryExecute(new Callable<Object>() {
- @Override
- public Object call() throws Exception {
- getService().setFolderPermissions(user.getId(), folder.getId(), perms);
- return null;
+ return getService().updateFolder(user.getId(), folder.getId(), fName, fPerms);
}
});
+ resp.getWriter().println(getNewUrl(req, folderUpdated));
}
} else {
final FileHeaderDTO file = (FileHeaderDTO) resource;
}
/**
+ * Returns the new URL of an updated folder.
+ */
+ private String getNewUrl(HttpServletRequest req, FolderDTO folder) throws UnsupportedEncodingException {
+ String parentUrl =URLDecoder.decode(getContextPath(req, true),"UTF-8");
+ String fpath = URLDecoder.decode(req.getPathInfo(), "UTF-8");
+ if (parentUrl.indexOf(fpath) != -1)
+ parentUrl = parentUrl.substring(0, parentUrl.indexOf(fpath));
+ if(!parentUrl.endsWith("/"))
+ parentUrl = parentUrl+"/";
+ parentUrl = parentUrl+folder.getOwner().getUsername()+PATH_FILES+folder.getPath();
+ return parentUrl;
+ }
+
+ /**
* Helper method to convert a JSON array of permissions into a set of
* PermissionDTO objects.
*
private long fileSize = -100;
- private long tenKBRead = -1;
-
private Long userId;
private String filename;