mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 09:46:17 +00:00
#41: Quota increase/decrease when file is added/delete
+ java.nio-ization
This commit is contained in:
@@ -228,7 +228,7 @@ public class UserDao {
|
||||
Map<String, Object> parameterMap = new HashMap<String, Object>();
|
||||
List<String> criteriaList = new ArrayList<String>();
|
||||
|
||||
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3");
|
||||
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3, u.USE_STORAGECURRENT_N as c4, u.USE_STORAGEQUOTA_N as c5");
|
||||
sb.append(" from T_USER u ");
|
||||
|
||||
// Add search criterias
|
||||
@@ -257,6 +257,8 @@ public class UserDao {
|
||||
userDto.setUsername((String) o[i++]);
|
||||
userDto.setEmail((String) o[i++]);
|
||||
userDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
|
||||
userDto.setStorageCurrent(((Number) o[i++]).longValue());
|
||||
userDto.setStorageQuota(((Number) o[i++]).longValue());
|
||||
userDtoList.add(userDto);
|
||||
}
|
||||
paginatedList.setResultList(userDtoList);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.sismics.docs.core.dao.jpa.dto;
|
||||
|
||||
|
||||
/**
|
||||
* User DTO.
|
||||
*
|
||||
@@ -26,6 +27,16 @@ public class UserDto {
|
||||
*/
|
||||
private Long createTimestamp;
|
||||
|
||||
/**
|
||||
* Storage quota.
|
||||
*/
|
||||
private Long storageQuota;
|
||||
|
||||
/**
|
||||
* Storage current usage.
|
||||
*/
|
||||
private Long storageCurrent;
|
||||
|
||||
/**
|
||||
* Getter of id.
|
||||
*
|
||||
@@ -88,6 +99,22 @@ public class UserDto {
|
||||
public Long getCreateTimestamp() {
|
||||
return createTimestamp;
|
||||
}
|
||||
|
||||
public Long getStorageQuota() {
|
||||
return storageQuota;
|
||||
}
|
||||
|
||||
public void setStorageQuota(Long storageQuota) {
|
||||
this.storageQuota = storageQuota;
|
||||
}
|
||||
|
||||
public Long getStorageCurrent() {
|
||||
return storageCurrent;
|
||||
}
|
||||
|
||||
public void setStorageCurrent(Long storageCurrent) {
|
||||
this.storageCurrent = storageCurrent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter of createTimestamp.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.sismics.docs.core.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
@@ -56,10 +56,10 @@ public class IndexingService extends AbstractScheduledService {
|
||||
directory = new RAMDirectory();
|
||||
log.info("Using RAM Lucene storage");
|
||||
} else if (luceneStorageConfig.equals(Constants.LUCENE_DIRECTORY_STORAGE_FILE)) {
|
||||
File luceneDirectory = DirectoryUtil.getLuceneDirectory();
|
||||
Path luceneDirectory = DirectoryUtil.getLuceneDirectory();
|
||||
log.info("Using file Lucene storage: {}", luceneDirectory);
|
||||
try {
|
||||
directory = new SimpleFSDirectory(luceneDirectory, new SimpleFSLockFactory());
|
||||
directory = new SimpleFSDirectory(luceneDirectory.toFile(), new SimpleFSLockFactory());
|
||||
} catch (IOException e) {
|
||||
log.error("Error initializing Lucene index", e);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.sismics.docs.core.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
@@ -17,27 +20,31 @@ public class DirectoryUtil {
|
||||
*
|
||||
* @return Base data directory
|
||||
*/
|
||||
public static File getBaseDataDirectory() {
|
||||
File baseDataDir = null;
|
||||
public static Path getBaseDataDirectory() {
|
||||
Path baseDataDir = null;
|
||||
if (StringUtils.isNotBlank(EnvironmentUtil.getDocsHome())) {
|
||||
// If the docs.home property is set then use it
|
||||
baseDataDir = new File(EnvironmentUtil.getDocsHome());
|
||||
baseDataDir = Paths.get(EnvironmentUtil.getDocsHome());
|
||||
} else if (EnvironmentUtil.isUnitTest()) {
|
||||
// For unit testing, use a temporary directory
|
||||
baseDataDir = new File(System.getProperty("java.io.tmpdir"));
|
||||
baseDataDir = Paths.get(System.getProperty("java.io.tmpdir"));
|
||||
} else {
|
||||
// We are in a webapp environment and nothing is specified, use the default directory for this OS
|
||||
if (EnvironmentUtil.isUnix()) {
|
||||
baseDataDir = new File("/var/docs");
|
||||
baseDataDir = Paths.get("/var/docs");
|
||||
} if (EnvironmentUtil.isWindows()) {
|
||||
baseDataDir = new File(EnvironmentUtil.getWindowsAppData() + "\\Sismics\\Docs");
|
||||
baseDataDir = Paths.get(EnvironmentUtil.getWindowsAppData() + "\\Sismics\\Docs");
|
||||
} else if (EnvironmentUtil.isMacOs()) {
|
||||
baseDataDir = new File(EnvironmentUtil.getMacOsUserHome() + "/Library/Sismics/Docs");
|
||||
baseDataDir = Paths.get(EnvironmentUtil.getMacOsUserHome() + "/Library/Sismics/Docs");
|
||||
}
|
||||
}
|
||||
|
||||
if (baseDataDir != null && !baseDataDir.isDirectory()) {
|
||||
baseDataDir.mkdirs();
|
||||
if (baseDataDir != null && !Files.isDirectory(baseDataDir)) {
|
||||
try {
|
||||
Files.createDirectories(baseDataDir);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
return baseDataDir;
|
||||
@@ -48,7 +55,7 @@ public class DirectoryUtil {
|
||||
*
|
||||
* @return Database directory.
|
||||
*/
|
||||
public static File getDbDirectory() {
|
||||
public static Path getDbDirectory() {
|
||||
return getDataSubDirectory("db");
|
||||
}
|
||||
|
||||
@@ -57,7 +64,7 @@ public class DirectoryUtil {
|
||||
*
|
||||
* @return Lucene indexes directory.
|
||||
*/
|
||||
public static File getLuceneDirectory() {
|
||||
public static Path getLuceneDirectory() {
|
||||
return getDataSubDirectory("lucene");
|
||||
}
|
||||
|
||||
@@ -66,7 +73,7 @@ public class DirectoryUtil {
|
||||
*
|
||||
* @return Storage directory.
|
||||
*/
|
||||
public static File getStorageDirectory() {
|
||||
public static Path getStorageDirectory() {
|
||||
return getDataSubDirectory("storage");
|
||||
}
|
||||
|
||||
@@ -75,7 +82,7 @@ public class DirectoryUtil {
|
||||
*
|
||||
* @return Log directory.
|
||||
*/
|
||||
public static File getLogDirectory() {
|
||||
public static Path getLogDirectory() {
|
||||
return getDataSubDirectory("log");
|
||||
}
|
||||
|
||||
@@ -84,11 +91,15 @@ public class DirectoryUtil {
|
||||
*
|
||||
* @return Subdirectory
|
||||
*/
|
||||
private static File getDataSubDirectory(String subdirectory) {
|
||||
File baseDataDir = getBaseDataDirectory();
|
||||
File directory = new File(baseDataDir.getPath() + File.separator + subdirectory);
|
||||
if (!directory.isDirectory()) {
|
||||
directory.mkdirs();
|
||||
private static Path getDataSubDirectory(String subdirectory) {
|
||||
Path baseDataDir = getBaseDataDirectory();
|
||||
Path directory = baseDataDir.resolve(subdirectory);
|
||||
if (!Files.isDirectory(directory)) {
|
||||
try {
|
||||
Files.createDirectories(directory);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return directory;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.sismics.docs.core.util;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.CipherInputStream;
|
||||
@@ -132,7 +130,7 @@ public class FileUtil {
|
||||
*/
|
||||
public static void save(InputStream inputStream, File file, String privateKey) throws Exception {
|
||||
Cipher cipher = EncryptionUtil.getEncryptionCipher(privateKey);
|
||||
Path path = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId());
|
||||
Path path = DirectoryUtil.getStorageDirectory().resolve(file.getId());
|
||||
Files.copy(new CipherInputStream(inputStream, cipher), path);
|
||||
|
||||
// Generate file variations
|
||||
@@ -172,21 +170,15 @@ public class FileUtil {
|
||||
image.flush();
|
||||
|
||||
// Write "web" encrypted image
|
||||
java.io.File outputFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_web").toFile();
|
||||
OutputStream outputStream = new CipherOutputStream(new FileOutputStream(outputFile), cipher);
|
||||
try {
|
||||
Path outputFile = DirectoryUtil.getStorageDirectory().resolve(file.getId() + "_web");
|
||||
try (OutputStream outputStream = new CipherOutputStream(Files.newOutputStream(outputFile), cipher)) {
|
||||
ImageUtil.writeJpeg(web, outputStream);
|
||||
} finally {
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
// Write "thumb" encrypted image
|
||||
outputFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile();
|
||||
outputStream = new CipherOutputStream(new FileOutputStream(outputFile), cipher);
|
||||
try {
|
||||
outputFile = DirectoryUtil.getStorageDirectory().resolve(file.getId() + "_thumb");
|
||||
try (OutputStream outputStream = new CipherOutputStream(Files.newOutputStream(outputFile), cipher)) {
|
||||
ImageUtil.writeJpeg(thumbnail, outputStream);
|
||||
} finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,20 +187,21 @@ public class FileUtil {
|
||||
* Remove a file from the storage filesystem.
|
||||
*
|
||||
* @param file File to delete
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void delete(File file) {
|
||||
java.io.File storedFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId()).toFile();
|
||||
java.io.File webFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_web").toFile();
|
||||
java.io.File thumbnailFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile();
|
||||
public static void delete(File file) throws IOException {
|
||||
Path storedFile = DirectoryUtil.getStorageDirectory().resolve(file.getId());
|
||||
Path webFile = DirectoryUtil.getStorageDirectory().resolve(file.getId() + "_web");
|
||||
Path thumbnailFile = DirectoryUtil.getStorageDirectory().resolve(file.getId() + "_thumb");
|
||||
|
||||
if (storedFile.exists()) {
|
||||
storedFile.delete();
|
||||
if (Files.exists(storedFile)) {
|
||||
Files.delete(storedFile);
|
||||
}
|
||||
if (webFile.exists()) {
|
||||
webFile.delete();
|
||||
if (Files.exists(webFile)) {
|
||||
Files.delete(webFile);
|
||||
}
|
||||
if (thumbnailFile.exists()) {
|
||||
thumbnailFile.delete();
|
||||
if (Files.exists(thumbnailFile)) {
|
||||
Files.delete(thumbnailFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
package com.sismics.util.jpa;
|
||||
|
||||
import com.sismics.docs.core.util.DirectoryUtil;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
@@ -8,15 +18,7 @@ import org.hibernate.service.ServiceRegistryBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import com.sismics.docs.core.util.DirectoryUtil;
|
||||
|
||||
/**
|
||||
* Entity manager factory.
|
||||
@@ -79,8 +81,8 @@ public final class EMF {
|
||||
log.info("Configuring EntityManager from environment parameters");
|
||||
Map<Object, Object> props = new HashMap<Object, Object>();
|
||||
props.put("hibernate.connection.driver_class", "org.h2.Driver");
|
||||
File dbDirectory = DirectoryUtil.getDbDirectory();
|
||||
String dbFile = dbDirectory.getAbsoluteFile() + File.separator + "docs";
|
||||
Path dbDirectory = DirectoryUtil.getDbDirectory();
|
||||
String dbFile = dbDirectory.resolve("docs").toAbsolutePath().toString();
|
||||
props.put("hibernate.connection.url", "jdbc:h2:file:" + dbFile + ";CACHE_SIZE=65536");
|
||||
props.put("hibernate.connection.username", "sa");
|
||||
props.put("hibernate.hbm2ddl.auto", "none");
|
||||
|
||||
Reference in New Issue
Block a user