Skip to content

Commit

Permalink
Set temp folder using environment variable NZBHYDRA_TEMP_FOLDER
Browse files Browse the repository at this point in the history
Closes #894
  • Loading branch information
theotherp committed Apr 1, 2024
1 parent 0708491 commit e6cfb43
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/org/nzbhydra/backup/BackupAndRestore.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.nzbhydra.config.ConfigReaderWriter;
import org.nzbhydra.genericstorage.GenericStorage;
import org.nzbhydra.logging.LoggingMarkers;
import org.nzbhydra.misc.TempFileProvider;
import org.nzbhydra.systemcontrol.SystemControl;
import org.nzbhydra.webaccess.HydraOkHttp3ClientHttpRequestFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -65,6 +66,8 @@ public class BackupAndRestore {
private HydraOkHttp3ClientHttpRequestFactory requestFactory;
@Autowired
private SystemControl systemControl;
@Autowired
private TempFileProvider tempFileProvider;

@Autowired
private GenericStorage genericStorage;
Expand Down Expand Up @@ -186,9 +189,10 @@ public List<BackupEntry> getExistingBackups() {

private void backupDatabase(File targetFile, boolean triggeredByUsed) {
final String tempPath;

File tempFile;
try {
tempFile = Files.createTempFile("nzbhydra", ".zip").toFile();
tempFile = tempFileProvider.getTempFile("databaseTest", ".zip");
tempPath = tempFile.getAbsolutePath().replace("\\", "/");
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -248,7 +252,7 @@ public GenericResponse restore(String filename) {

public GenericResponse restoreFromFile(InputStream inputStream) {
try {
File tempFile = File.createTempFile("nzbhydra-restore", ".zip");
File tempFile = tempFileProvider.getTempFile("restore", ".zip");
FileUtils.copyInputStreamToFile(inputStream, tempFile);
tempFile.deleteOnExit();
restoreFromFile(tempFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.nzbhydra.logging.LogAnonymizer;
import org.nzbhydra.logging.LogContentProvider;
import org.nzbhydra.logging.LoggingMarkers;
import org.nzbhydra.misc.TempFileProvider;
import org.nzbhydra.problemdetection.OutdatedWrapperDetector;
import org.nzbhydra.springnative.ReflectionMarker;
import org.nzbhydra.update.UpdateManager;
Expand Down Expand Up @@ -99,6 +100,8 @@ public class DebugInfosProvider {

private final List<TimeAndThreadCpuUsages> timeAndThreadCpuUsagesList = new ArrayList<>();
private final Map<String, Long> lastThreadCpuTimes = new HashMap<>();
@Autowired
private TempFileProvider tempFileProvider;


@PostConstruct
Expand Down Expand Up @@ -261,7 +264,7 @@ public File createDebugInfosZipFile() throws IOException {


String anonymizedLog = logAnonymizer.getAnonymizedLog(logContentProvider.getLog());
File tempFile = File.createTempFile("nzbhydradebuginfos", "zip");
File tempFile = tempFileProvider.getTempFile("debuginfos", ".zip");
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
try (ZipOutputStream zos = new ZipOutputStream(fos)) {
writeStringToZip(zos, "nzbhydra2.log", anonymizedLog.getBytes(StandardCharsets.UTF_8));
Expand Down Expand Up @@ -390,7 +393,7 @@ public static boolean isRunInDocker() {
@Transactional
public String executeSqlQuery(String sql) throws IOException {
logger.info("Executing SQL query \"{}\" and returning as CSV", sql);
File tempFile = File.createTempFile("nzbhydra", "csv");
File tempFile = tempFileProvider.getTempFile("dbquery", "csv");
String path = tempFile.getAbsolutePath().replace("\\", "/");
entityManager.createNativeQuery(String.format("CALL CSVWRITE('%s', '%s')", path, sql.replace("'", "''"))).executeUpdate();
return new String(Files.readAllBytes(tempFile.toPath()));
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/org/nzbhydra/downloading/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.nzbhydra.indexers.IndexerApiAccessType;
import org.nzbhydra.indexers.NfoResult;
import org.nzbhydra.logging.LoggingMarkers;
import org.nzbhydra.misc.TempFileProvider;
import org.nzbhydra.notifications.DownloadNotificationEvent;
import org.nzbhydra.searching.SearchModuleProvider;
import org.nzbhydra.searching.db.SearchResultEntity;
Expand Down Expand Up @@ -82,6 +83,8 @@ public class FileHandler {
private IndexerSpecificDownloadExceptions indexerSpecificDownloadExceptions;

private final Set<File> temporaryZipFiles = new HashSet<>();
@Autowired
private TempFileProvider tempFileProvider;

public DownloadResult getFileByGuid(long guid, SearchSource accessSource) throws InvalidSearchResultIdException {
final SearchResultEntity searchResult = getResultFromGuid(guid, accessSource);
Expand Down Expand Up @@ -282,7 +285,7 @@ private NzbsDownload getNzbsAsFiles(Collection<Long> guids, Path targetDirectory
public File createZip(List<File> nzbFiles) throws Exception {
logger.info("Creating ZIP with files");

File tempFile = File.createTempFile("nzbhydra", ".zip");
File tempFile = tempFileProvider.getTempFile("nzbs", ".zip");
temporaryZipFiles.add(tempFile);
tempFile.deleteOnExit();
logger.debug("Using temp file {}", tempFile.getAbsolutePath());
Expand Down
49 changes: 49 additions & 0 deletions core/src/main/java/org/nzbhydra/misc/TempFileProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* (C) Copyright 2024 TheOtherP ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.nzbhydra.misc;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@Configuration
public class TempFileProvider {

private static final Logger logger = LoggerFactory.getLogger(TempFileProvider.class);

@Autowired
private ConfigurableEnvironment environment;

public File getTempFile(String postFix, String extension) throws IOException {
String nzbhydraTempFolder = environment.getProperty("NZBHYDRA_TEMP_FOLDER", String.class);
if (nzbhydraTempFolder != null) {
logger.info("Using temp folder {} defined by property NZBHYDRA_TEMP_FOLDER", nzbhydraTempFolder);
return new File(nzbhydraTempFolder, "nzbhydra" + RandomStringUtils.randomAlphanumeric(5) + extension);
} else {
return Files.createTempFile("nzbhydra-" + postFix, extension).toFile();
}
}


}
2 changes: 2 additions & 0 deletions core/src/main/resources/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
changes:
- type: "feature"
text: "I've completely rewritten the wrapper for windows. It's not a packaged python script anymore but an actually compiled binary. This has a couple of benefits: No more false positives from virus scanners (VirusTotal shows 5/72). No more temporary files not being cleaned up after a crash. The files are (a bit) smaller.\nI don't plan to compile the wrapper for anything but windows as it makes more sense and is easier to run the python wrapper instead."
- type: "feature"
text: "The temp folder can be set using the environment variable NZBHYDRA_TEMP_FOLDER. See #984"
- type: "fix"
text: "IPs or usernames were not shown in the history. See #932"
- type: "fix"
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/resources/wrapperHashes2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"2e772a1a140820687d5466e27c030d36f82abb54",
"930be2ca00604918fc0653e22e118efdc1693eb0",
"ecadc9b8264968a2ca53cedb73f00a69b4172315",
"847cfdc8a7014c660b47c92e3f55b38b2fdc15ac",
"15508e8047647e5e6c971b7fde5f81a907a9b358",
"709e66a3c3267f0ebdf6b37a855de1571037e150"
"4ece63cc425c88efbace9a013c3e1203a63b40c0",
"847cfdc8a7014c660b47c92e3f55b38b2fdc15ac"
]

0 comments on commit e6cfb43

Please sign in to comment.