Skip to content

Commit dc6c495

Browse files
committed
Added check for max docs per folder
1 parent 8c33583 commit dc6c495

File tree

7 files changed

+79
-11
lines changed

7 files changed

+79
-11
lines changed

logicaldoc-core/src/main/java/com/logicaldoc/core/document/HibernateDocumentDAO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ public void store(Document doc, final DocumentHistory transaction) throws Persis
304304
log.debug("Aspect customId is disabled so force the the Custom ID to a random UUID");
305305
}
306306

307+
/*
308+
* Check maximum number of documents per folder
309+
*/
310+
long maxDocsPerFolder = config.getLong("maxdocsperfolder", -1L);
311+
if (doc.getId() == 0L && maxDocsPerFolder > 0) {
312+
long count = folderDAO.countDocs(doc.getFolder().getId());
313+
if (count >= maxDocsPerFolder)
314+
throw new TooManyDocumentsException(doc.getFolder(), maxDocsPerFolder);
315+
}
316+
307317
log.debug("Invoke listeners before store");
308318
Map<String, Object> dictionary = new HashMap<>();
309319
for (DocumentListener listener : listenerManager.getListeners())
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.logicaldoc.core.document;
2+
3+
import com.logicaldoc.core.PersistenceException;
4+
import com.logicaldoc.core.folder.Folder;
5+
6+
/**
7+
* Exception raised when you try to add more documents than allowed in the same
8+
* folder
9+
*
10+
* @author Marco Meschieri - LogicalDOC
11+
* @since 9.1.1
12+
*/
13+
public class TooManyDocumentsException extends PersistenceException {
14+
private static final long serialVersionUID = 1L;
15+
16+
private final long folderId;
17+
18+
public TooManyDocumentsException(Folder folder, long max) {
19+
super(String.format("Folder %s (%d) cannot exceed the limit of %d documents configured in the settings",
20+
folder.getName(), folder.getId(), max));
21+
this.folderId = folder.getId();
22+
}
23+
24+
public long getFolderId() {
25+
return folderId;
26+
}
27+
}

logicaldoc-core/src/main/java/com/logicaldoc/core/folder/FolderDAO.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public interface FolderDAO extends PersistentObjectDAO<Folder> {
5353
*
5454
* @throws PersistenceException error at data layer
5555
*/
56-
public List<Folder> findByName(Folder parent, String name, Long tenantId, boolean caseSensitive) throws PersistenceException;
56+
public List<Folder> findByName(Folder parent, String name, Long tenantId, boolean caseSensitive)
57+
throws PersistenceException;
5758

5859
/**
5960
* Retrieves the root folder of the given tenant
@@ -207,7 +208,7 @@ public Collection<Long> findFolderIdByUserIdAndPermission(long userId, Permissio
207208
*
208209
* @return List of found folders
209210
*
210-
* @throws PersistenceException Error in the database
211+
* @throws PersistenceException Error in the database
211212
*/
212213
public List<Folder> findChildren(long parentId, Integer max) throws PersistenceException;
213214

@@ -243,12 +244,12 @@ public Collection<Long> findFolderIdByUserIdAndPermission(long userId, Permissio
243244
*
244245
* @return if the user has write permission
245246
*
246-
* @throws PersistenceException error at data layer
247+
* @throws PersistenceException error at data layer
247248
*/
248249
public boolean hasWriteAccess(Folder folder, long userId) throws PersistenceException;
249-
250+
250251
public boolean isReadAllowed(long folderId, long userId) throws PersistenceException;
251-
252+
252253
public boolean isPreviewAllowed(long folderId, long userId) throws PersistenceException;
253254

254255
public boolean isPrintAllowed(long folderId, long userId) throws PersistenceException;
@@ -293,7 +294,7 @@ public Collection<Long> findFolderIdByUserIdAndPermission(long userId, Permissio
293294
*
294295
* @return List of selected folder ID's.
295296
*
296-
* @throws PersistenceException error at data layer
297+
* @throws PersistenceException error at data layer
297298
*/
298299
public List<Long> findIdByUserId(long userId, long parentId) throws PersistenceException;
299300

@@ -304,7 +305,7 @@ public Collection<Long> findFolderIdByUserIdAndPermission(long userId, Permissio
304305
*
305306
* @return The List of folders
306307
*
307-
* @throws PersistenceException error at data layer
308+
* @throws PersistenceException error at data layer
308309
*/
309310
public List<Folder> findByGroupId(long groupId) throws PersistenceException;
310311

@@ -647,7 +648,7 @@ public Folder copy(Folder source, Folder target, String newName, boolean folders
647648
*
648649
* @return True if the folder with the given parentId is parent of the
649650
* folder with the given childId
650-
*
651+
*
651652
* @throws PersistenceException error at data layer
652653
*/
653654
public boolean isInPath(long parentId, long childId) throws PersistenceException;
@@ -774,6 +775,15 @@ public void updateSecurityRef(long folderId, long rightsFolderId, FolderHistory
774775
*/
775776
public long countDocsInTree(long rootId);
776777

778+
/**
779+
* Counts the number of documents inside a given folder
780+
*
781+
* @param folderId identifier of the folder
782+
*
783+
* @return the number of documents contained in the folder
784+
*/
785+
public long countDocs(long folderId);
786+
777787
/**
778788
* Counts the number of documents inside a given folder's tree (direct and
779789
* indirect children)

logicaldoc-core/src/main/java/com/logicaldoc/core/folder/HibernateFolderDAO.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,18 @@ public long countDocsInTree(long rootId) {
20152015
return 0;
20162016
}
20172017
}
2018-
2018+
2019+
@Override
2020+
public long countDocs(long folderId) {
2021+
try {
2022+
Folder folder= findFolder(folderId);
2023+
return queryForLong("SELECT COUNT(*) from ld_document WHERE ld_deleted=0 and ld_folderid="+folder.getId());
2024+
} catch (PersistenceException e) {
2025+
log.error(e.getMessage(), e);
2026+
return 0;
2027+
}
2028+
}
2029+
20192030
@Override
20202031
public long computeTreeSize(long rootId) {
20212032
try {

logicaldoc-core/src/test/java/com/logicaldoc/core/folder/HibernateFolderDAOTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public void testCountDocsInTree() throws PersistenceException {
184184
assertEquals(0, count);
185185
}
186186

187+
@Test
188+
public void testCountDocs() throws PersistenceException {
189+
long count = testSubject.countDocs(6L);
190+
assertEquals(4, count);
191+
}
192+
187193
@Test
188194
public void testComputeTreeSize() throws PersistenceException {
189195
/*

logicaldoc-webapp/src/main/resources/context.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,6 @@ gravatar.enabled = true
918918

919919
onlyoffice.url=
920920
onlyoffice.security.key=
921-
onlyoffice.callback=
921+
onlyoffice.callback=
922+
923+
maxdocsperfolder = 10000

logicaldoc-webapp/src/test/resources/contexttest.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,6 @@ default.anonymous.key=12qwe3gre6k
851851
default.anonymous.user=anonymous
852852

853853
throttle.username.max = 1
854-
throttle.username.wait = 30
854+
throttle.username.wait = 30
855+
856+
maxdocsperfolder = 1000

0 commit comments

Comments
 (0)