Skip to content

Commit ad34ba1

Browse files
juanjmeronoottenhoff
authored andcommitted
SAK-31743 Restoring files from trash can get quota exceeded (sakaiproject#3318)
1 parent 1bcb3da commit ad34ba1

File tree

2 files changed

+85
-19
lines changed

2 files changed

+85
-19
lines changed

kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/BaseContentService.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -4658,6 +4658,24 @@ public void restoreResource(String id) throws PermissionException, IdUsedExcepti
46584658
newResource.setResourceType(deleResource.getResourceType());
46594659
newResource.setAvailability(deleResource.isHidden(), deleResource.getReleaseDate(),deleResource.getRetractDate());
46604660
newResource.setContent(m_storage.streamDeletedResourceBody(deleResource));
4661+
try {
4662+
// If you're storing the file in DB this breaks as it removes the restored file.
4663+
removeDeletedResource(deleResource);
4664+
// close the edit object
4665+
((BaseResourceEdit) deleResource).closeEdit();
4666+
} catch (PermissionException pe) {
4667+
M_log.error("restoreResource: access to resource not permitted" + id, pe);
4668+
try
4669+
{
4670+
removeResource(newResource.getId());
4671+
}
4672+
catch(Exception e1)
4673+
{
4674+
// ignore -- no need to remove the resource if it doesn't exist
4675+
M_log.debug("Unable to remove partially completed resource: " + deleResource.getId() + "\n" + e1);
4676+
}
4677+
throw pe;
4678+
}
46614679
try {
46624680
addProperties(newResource.getPropertiesEdit(), deleResource.getProperties());
46634681
commitResource(newResource, NotificationService.NOTI_NONE);
@@ -4687,24 +4705,6 @@ public void restoreResource(String id) throws PermissionException, IdUsedExcepti
46874705
}
46884706
throw e;
46894707
}
4690-
try {
4691-
// If you're storing the file in DB this breaks as it removes the restored file.
4692-
removeDeletedResource(deleResource);
4693-
// close the edit object
4694-
((BaseResourceEdit) deleResource).closeEdit();
4695-
} catch (PermissionException pe) {
4696-
M_log.error("restoreResource: access to resource not permitted" + id, pe);
4697-
try
4698-
{
4699-
removeResource(newResource.getId());
4700-
}
4701-
catch(Exception e1)
4702-
{
4703-
// ignore -- no need to remove the resource if it doesn't exist
4704-
M_log.debug("Unable to remove partially completed resource: " + deleResource.getId() + "\n" + e1);
4705-
}
4706-
throw pe;
4707-
}
47084708
} catch (IdUnusedException iue) {
47094709
M_log.error("restoreResource: cannot locate deleted resource " + id, iue);
47104710
throw iue;

kernel/kernel-impl/src/test/java/org/sakaiproject/content/impl/test/ContentHostingServiceRecycleTest.java

+67-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
import org.junit.Assert;
1313
import org.junit.BeforeClass;
1414
import org.junit.Test;
15+
import org.sakaiproject.content.api.ContentCollection;
16+
import org.sakaiproject.content.api.ContentCollectionEdit;
1517
import org.sakaiproject.content.api.ContentHostingService;
1618
import org.sakaiproject.content.api.ContentResource;
1719
import org.sakaiproject.content.api.ContentResourceEdit;
20+
import org.sakaiproject.entity.api.ResourceProperties;
21+
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
22+
import org.sakaiproject.exception.OverQuotaException;
1823
import org.sakaiproject.exception.IdUnusedException;
1924
import org.sakaiproject.exception.IdUsedException;
2025
import org.sakaiproject.test.SakaiKernelTestBase;
@@ -27,7 +32,8 @@
2732
*/
2833
public class ContentHostingServiceRecycleTest extends SakaiKernelTestBase {
2934
private static Logger log = LoggerFactory.getLogger(ContentHostingServiceRecycleTest.class);
30-
35+
private static final String SAMPLE_FOLDER = "/user/admin/";
36+
3137
@BeforeClass
3238
public static void beforeClass() {
3339
try {
@@ -146,6 +152,66 @@ public void testDeleteResourceRestoreOnTop() throws Exception {
146152
ch.removeResource(filename);
147153
}
148154

155+
/**
156+
* This is to check that when a restore is attempted and the file exceed quota
157+
* the file is not restored and we correctly unlock it.
158+
* @throws Exception
159+
*/
160+
@Test
161+
public void testRestoreOnOverquota() throws Exception {
162+
ContentHostingService ch = getService(ContentHostingService.class);
163+
SessionManager sm = getService(SessionManager.class);
164+
ThreadLocalManager tl = getService(ThreadLocalManager.class);
165+
reset(tl, sm);
166+
167+
// Set quota to 1kb
168+
ResourcePropertiesEdit props = ch.newResourceProperties();
169+
props.addProperty (ResourceProperties.PROP_COLLECTION_BODY_QUOTA,"1");
170+
ContentCollection c = ch.addCollection(SAMPLE_FOLDER,props);
171+
ContentCollectionEdit ce = ch.editCollection(SAMPLE_FOLDER);
172+
ch.commitCollection(ce);
173+
174+
long quota = ch.getQuota(ch.getCollection(SAMPLE_FOLDER));
175+
Assert.assertEquals("The quota is set to 1",1,quota);
176+
177+
// Create a file
178+
String filename = SAMPLE_FOLDER + UUID.randomUUID().toString();
179+
180+
try {
181+
ContentResourceEdit resource = ch.addResource(filename);
182+
resource.setContent(new byte[1048]);
183+
ch.commitResource(resource);
184+
Assert.fail("We should have exceed the quota.");
185+
} catch (OverQuotaException oqe) {
186+
// OverQuota Resource Goes to Trash
187+
}
188+
189+
try {
190+
ch.getResource(filename);
191+
Assert.fail("We shouldn't be able to find: "+ filename);
192+
} catch (IdUnusedException e) {
193+
// Expected
194+
}
195+
196+
try {
197+
ch.restoreResource(filename);
198+
Assert.fail("We shouldn't be able to restore: "+ filename);
199+
} catch (OverQuotaException e) {
200+
// Expected
201+
}
202+
203+
try {
204+
ch.getResource(filename);
205+
Assert.fail("We shouldn't be able to find: "+ filename);
206+
} catch (IdUnusedException e) {
207+
// Expected
208+
}
209+
210+
List<ContentResource> allDeleted = ch.getAllDeletedResources(SAMPLE_FOLDER);
211+
Assert.assertEquals("There should only be one copy of the file in the recycle bin.", 1, allDeleted.size());
212+
213+
}
214+
149215
/**
150216
* Clear out any threadlocals and reset the session to be admin.
151217
* @param tl ThreadLocalManager service.

0 commit comments

Comments
 (0)