From d5e73ecd8bb30f1bf98f0aef57d67f0b4e1ae1f9 Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Thu, 2 May 2019 11:13:16 +0200 Subject: [PATCH] Closes #308: update user quota when deleting a document --- .../docs/rest/resource/DocumentResource.java | 21 ++++++++-- .../sismics/docs/rest/TestFileResource.java | 42 +++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java index 7de9134f..5d8f483f 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java @@ -960,15 +960,30 @@ public class DocumentResource extends BaseResource { // Delete the document documentDao.delete(id, principal.getId()); - - // Raise file deleted events (don't bother sending document updated event) + + long totalSize = 0L; 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.setUserId(principal.getId()); fileDeletedAsyncEvent.setFile(file); 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 DocumentDeletedAsyncEvent documentDeletedAsyncEvent = new DocumentDeletedAsyncEvent(); documentDeletedAsyncEvent.setUserId(principal.getId()); diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java index 126252bf..b86c9939 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java @@ -532,5 +532,47 @@ public class TestFileResource extends BaseJerseyTest { .cookie(TokenBasedSecurityFilter.COOKIE_NAME, fileQuotaToken) .get(JsonObject.class); 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()); } } \ No newline at end of file