Directory and database table clearing

- Code quality improvement
This commit is contained in:
Max W. 2024-02-04 22:03:30 +01:00
parent b2e81ff5e1
commit 05720583ba
3 changed files with 18 additions and 13 deletions

View File

@ -56,26 +56,29 @@ public class RethinkDBService {
// rethinkdb check if database exists // rethinkdb check if database exists
try { try {
r.dbCreate(config.getDatabase()).run(connection).stream(); r.dbCreate(config.getDatabase()).run(connection).stream();
log.info("Database " + config.getDatabase() + " created"); log.debug("Database " + config.getDatabase() + " created");
} catch (ReqlOpFailedError e) { } catch (ReqlOpFailedError e) {
log.info("Database " + config.getDatabase() + " already exists. Error: " + e.getClass().getSimpleName()); log.debug("Database " + config.getDatabase() + " already exists. Error: " + e.getClass().getSimpleName());
} }
// rethinkdb check if table file_uploads exists // rethinkdb check if table file_uploads exists
try { try {
r.db(config.getDatabase()).tableCreate("file_uploads").run(connection).stream(); r.db(config.getDatabase()).tableCreate("file_uploads").run(connection).stream();
log.info("Table 'file_uploads' created successfully."); log.debug("Table 'file_uploads' created successfully.");
} catch (ReqlOpFailedError e) { } catch (ReqlOpFailedError e) {
log.info("Table 'file_uploads' already exists. No action needed."); log.debug("Table 'file_uploads' already exists. Clearing content...");
r.db(config.getDatabase()).table("file_uploads").delete().run(connection);
log.debug("Table 'file_uploads' cleared successfully.");
} }
// rethinkdb check if table id_store exists // rethinkdb check if table id_store exists
try { try {
r.db(config.getDatabase()).tableCreate("id_store").run(connection).stream(); r.db(config.getDatabase()).tableCreate("id_store").run(connection).stream();
log.info("Table 'id_store' created successfully."); log.debug("Table 'id_store' created successfully.");
} catch (ReqlOpFailedError e) { } catch (ReqlOpFailedError e) {
log.info("Table 'id_store' already exists. No action needed."); log.debug("Table 'id_store' already exists. No action needed.");
} }
log.info("Database ready for operation!");
} }
@PreDestroy @PreDestroy

View File

@ -5,6 +5,7 @@ import de.w665.sharepulse.model.FileUpload;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@ -18,10 +19,8 @@ public class TestRestResource extends ApiRestController {
} }
@GetMapping("test") @GetMapping("test")
public String test() { public String test(@RequestParam String id) {
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id);
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId("2402041"); return fileUpload != null ? fileUpload.toString() : "FileUpload not found for id: " + id;
return fileUpload.toString();
} }
} }

View File

@ -5,6 +5,7 @@ import de.w665.sharepulse.model.FileUpload;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -66,13 +67,15 @@ public class FileService {
@PostConstruct @PostConstruct
public void createFolder() { public void createFolder() {
Path path = Paths.get(getTempDirPath()); Path path = Paths.get(getTempDirPath());
log.debug("Creating directory " + path.getFileName()); log.debug("Checking temp file directory " + path.getFileName());
try { try {
if (!Files.exists(path)) { if (!Files.exists(path)) {
Files.createDirectory(path); Files.createDirectory(path);
log.debug("Directory created"); log.debug("Directory created");
} else { } else {
log.debug("Directory already exists"); log.debug("Directory already exists. Clearing content.");
FileUtils.cleanDirectory(new File(getTempDirPath()));
log.debug("Directory content cleared.");
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();