mirror of
https://github.com/sismics/docs.git
synced 2024-11-25 23:27:57 +01:00
Closes #308: update user quota when deleting a document
This commit is contained in:
parent
2235a0498b
commit
d5e73ecd8b
@ -961,14 +961,29 @@ public class DocumentResource extends BaseResource {
|
|||||||
// Delete the document
|
// Delete the document
|
||||||
documentDao.delete(id, principal.getId());
|
documentDao.delete(id, principal.getId());
|
||||||
|
|
||||||
// Raise file deleted events (don't bother sending document updated event)
|
long totalSize = 0L;
|
||||||
for (File file : fileList) {
|
for (File file : fileList) {
|
||||||
|
// Store the file size to update the quota
|
||||||
|
java.nio.file.Path storedFile = DirectoryUtil.getStorageDirectory().resolve(file.getId());
|
||||||
|
try {
|
||||||
|
totalSize += Files.size(storedFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// The file doesn't exists on disk, which is weird, but not fatal
|
||||||
|
}
|
||||||
|
|
||||||
|
// Raise file deleted event
|
||||||
FileDeletedAsyncEvent fileDeletedAsyncEvent = new FileDeletedAsyncEvent();
|
FileDeletedAsyncEvent fileDeletedAsyncEvent = new FileDeletedAsyncEvent();
|
||||||
fileDeletedAsyncEvent.setUserId(principal.getId());
|
fileDeletedAsyncEvent.setUserId(principal.getId());
|
||||||
fileDeletedAsyncEvent.setFile(file);
|
fileDeletedAsyncEvent.setFile(file);
|
||||||
ThreadLocalContext.get().addAsyncEvent(fileDeletedAsyncEvent);
|
ThreadLocalContext.get().addAsyncEvent(fileDeletedAsyncEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the user quota
|
||||||
|
UserDao userDao = new UserDao();
|
||||||
|
User user = userDao.getById(principal.getId());
|
||||||
|
user.setStorageCurrent(user.getStorageCurrent() - totalSize);
|
||||||
|
userDao.updateQuota(user);
|
||||||
|
|
||||||
// Raise a document deleted event
|
// Raise a document deleted event
|
||||||
DocumentDeletedAsyncEvent documentDeletedAsyncEvent = new DocumentDeletedAsyncEvent();
|
DocumentDeletedAsyncEvent documentDeletedAsyncEvent = new DocumentDeletedAsyncEvent();
|
||||||
documentDeletedAsyncEvent.setUserId(principal.getId());
|
documentDeletedAsyncEvent.setUserId(principal.getId());
|
||||||
|
@ -532,5 +532,47 @@ public class TestFileResource extends BaseJerseyTest {
|
|||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
||||||
.get(JsonObject.class);
|
.get(JsonObject.class);
|
||||||
Assert.assertEquals(585282L, json.getJsonNumber("storage_current").longValue());
|
Assert.assertEquals(585282L, json.getJsonNumber("storage_current").longValue());
|
||||||
|
|
||||||
|
// Create a document
|
||||||
|
long create1Date = new Date().getTime();
|
||||||
|
json = target().path("/document").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
||||||
|
.put(Entity.form(new Form()
|
||||||
|
.param("title", "File test document 1")
|
||||||
|
.param("language", "eng")
|
||||||
|
.param("create_date", Long.toString(create1Date))), JsonObject.class);
|
||||||
|
String document1Id = json.getString("id");
|
||||||
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
|
// Add a file to this document (163510 bytes large)
|
||||||
|
try (InputStream is = Resources.getResource("file/PIA00452.jpg").openStream()) {
|
||||||
|
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "PIA00452.jpg");
|
||||||
|
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
||||||
|
target()
|
||||||
|
.register(MultiPartFeature.class)
|
||||||
|
.path("/file").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
||||||
|
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
||||||
|
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check current quota
|
||||||
|
json = target().path("/user").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
||||||
|
.get(JsonObject.class);
|
||||||
|
Assert.assertEquals(748792, json.getJsonNumber("storage_current").longValue());
|
||||||
|
|
||||||
|
// Deletes the document
|
||||||
|
json = target().path("/document/" + document1Id).request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
||||||
|
.delete(JsonObject.class);
|
||||||
|
Assert.assertEquals("ok", json.getString("status"));
|
||||||
|
|
||||||
|
// Check current quota
|
||||||
|
json = target().path("/user").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken)
|
||||||
|
.get(JsonObject.class);
|
||||||
|
Assert.assertEquals(585282L, json.getJsonNumber("storage_current").longValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user