1
0
mirror of https://github.com/sismics/docs.git synced 2026-01-07 22:09:29 +00:00

File sharing (in progress)

This commit is contained in:
jendib
2013-08-13 23:15:58 +02:00
parent 99978a12b7
commit 5cfc487193
6 changed files with 242 additions and 34 deletions

View File

@@ -54,10 +54,11 @@ public class FileResource extends BaseResource {
}
FileDao fileDao = new FileDao();
DocumentDao documentDao = new DocumentDao();
File fileDb;
try {
fileDb = fileDao.getFile(id);
// TODO Check that the current user owns the document linked to this file
documentDao.getDocument(fileDb.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
}
@@ -241,15 +242,17 @@ public class FileResource extends BaseResource {
// Get the file
FileDao fileDao = new FileDao();
DocumentDao documentDao = new DocumentDao();
File file;
try {
file = fileDao.getFile(id);
// TODO Check that the current user owns the document linked to this file
documentDao.getDocument(file.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
}
// Delete the document
// Delete the file
// TODO Delete the file from storage too
fileDao.delete(file.getId());
// Always return ok
@@ -277,10 +280,11 @@ public class FileResource extends BaseResource {
// Get the file
FileDao fileDao = new FileDao();
DocumentDao documentDao = new DocumentDao();
File file;
try {
file = fileDao.getFile(id);
// TODO Check that the current user owns the document linked to this file
documentDao.getDocument(file.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
}

View File

@@ -1,7 +1,11 @@
package com.sismics.docs.rest.resource;
import javax.ws.rs.Consumes;
import java.text.MessageFormat;
import javax.persistence.NoResultException;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@@ -12,16 +16,21 @@ import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.FileShareDao;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.FileShare;
import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil;
import com.sun.jersey.multipart.FormDataParam;
/**
* File share REST resources.
*
* @author bgamard
*/
@Path("/share")
@Path("/fileshare")
public class FileShareResource extends BaseResource {
/**
* Add a file share to a file.
@@ -32,10 +41,9 @@ public class FileShareResource extends BaseResource {
* @throws JSONException
*/
@PUT
@Consumes("multipart/form-data")
@Produces(MediaType.APPLICATION_JSON)
public Response add(
@FormDataParam("id") String fileId) throws JSONException {
@FormParam("id") String fileId) throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
@@ -44,19 +52,32 @@ public class FileShareResource extends BaseResource {
ValidationUtil.validateRequired(fileId, "id");
// Get the file
// TODO Not implemented
FileDao fileDao = new FileDao();
DocumentDao documentDao = new DocumentDao();
try {
File file = fileDao.getFile(fileId);
documentDao.getDocument(file.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", fileId));
}
// Create the file share
FileShareDao fileShareDao = new FileShareDao();
FileShare fileShare = new FileShare();
fileShare.setFileId(fileId);
fileShareDao.create(fileShare);
// Always return ok
JSONObject response = new JSONObject();
response.put("status", "ok");
response.put("id", fileId);
response.put("id", fileShare.getId());
return Response.ok().entity(response).build();
}
/**
* Deletes a file share.
*
* @param id File shqre ID
* @param id File share ID
* @return Response
* @throws JSONException
*/
@@ -69,20 +90,22 @@ public class FileShareResource extends BaseResource {
throw new ForbiddenClientException();
}
// Get the file shqre
// FileShareDao fileShareDao = new FileShareDao();
// FileShare fileShare;
// try {
// fileShare = fileShareDao.getFileShare(id);
// } catch (NoResultException e) {
// throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
// }
//
// // Delete the file share
// fileShareDao.delete(fileShare.getId());
// TODO Not implemented
// Get the file share
FileShareDao fileShareDao = new FileShareDao();
FileDao fileDao = new FileDao();
DocumentDao documentDao = new DocumentDao();
FileShare fileShare;
try {
fileShare = fileShareDao.getFileShare(id);
File file = fileDao.getFile(fileShare.getFileId());
documentDao.getDocument(file.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileShareNotFound", MessageFormat.format("File share not found: {0}", id));
}
// Delete the file share
fileShareDao.delete(fileShare.getId());
// Always return ok
JSONObject response = new JSONObject();
response.put("status", "ok");