-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from bcgov/develop/alex-GRAD2-2410-2
Develop/alex grad2 2410 2
- Loading branch information
Showing
7 changed files
with
136 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/IOUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ca.bc.gov.educ.api.gradbusiness.util; | ||
|
||
import org.apache.commons.lang3.SystemUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.FileSystemUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.attribute.FileAttribute; | ||
import java.nio.file.attribute.PosixFilePermission; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import java.util.Set; | ||
|
||
public class IOUtils { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(IOUtils.class); | ||
|
||
private IOUtils(){} | ||
|
||
/** | ||
* Creates a secured temp dir for processing files, it is up to | ||
* calling method to also remove directory (see removeFileOrDirectory | ||
* method in this class) | ||
* | ||
* @param location | ||
* @param prefix | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static File createTempDirectory(String location, String prefix) throws IOException { | ||
File temp; | ||
Path loc = Paths.get(location); | ||
if (SystemUtils.IS_OS_UNIX) { | ||
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")); | ||
temp = Files.createTempDirectory(loc, prefix, attr).toFile(); // Compliant | ||
} else { | ||
temp = Files.createTempDirectory(loc, prefix).toFile(); // Compliant | ||
temp.setReadable(true, true); | ||
temp.setWritable(true, true); | ||
temp.setExecutable(true, true); | ||
} | ||
return temp; | ||
} | ||
|
||
/** | ||
* Removes a directory or file recursively | ||
* @param file | ||
*/ | ||
public static void removeFileOrDirectory(File file) { | ||
try { | ||
if(file.isDirectory() && file.exists()){ | ||
FileSystemUtils.deleteRecursively(file); | ||
} else { | ||
Files.deleteIfExists(Path.of(file.getAbsolutePath())); | ||
} | ||
} catch (IOException e) { | ||
logger.error("Unable to delete file or folder {}", file.getAbsolutePath()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters