forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/dspace-cris-2023_02_x' into main-cris
# Conflicts: # dspace-api/pom.xml # dspace-api/src/main/java/org/dspace/content/authority/RorOrgUnitAuthority.java # dspace-iiif/pom.xml # dspace-oai/pom.xml # dspace-rdf/pom.xml # dspace-rest/pom.xml # dspace-server-webapp/pom.xml # dspace-services/pom.xml # dspace-sword/pom.xml # dspace-swordv2/pom.xml # dspace/modules/additions/pom.xml # dspace/modules/pom.xml # dspace/modules/rest/pom.xml # dspace/modules/server-boot/pom.xml # dspace/modules/server/pom.xml # dspace/pom.xml # pom.xml
- Loading branch information
Showing
103 changed files
with
5,169 additions
and
698 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
125 changes: 0 additions & 125 deletions
125
dspace-api/src/main/java/org/dspace/app/bulkimport/util/BulkImportFileUtil.java
This file was deleted.
Oops, something went wrong.
181 changes: 181 additions & 0 deletions
181
dspace-api/src/main/java/org/dspace/app/bulkimport/util/ImportFileUtil.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,181 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
|
||
package org.dspace.app.bulkimport.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.dspace.scripts.handler.DSpaceRunnableHandler; | ||
import org.dspace.services.factory.DSpaceServicesFactory; | ||
|
||
/* | ||
* @author Jurgen Mamani | ||
*/ | ||
public class ImportFileUtil { | ||
|
||
private static final Logger log = LogManager.getLogger(ImportFileUtil.class); | ||
|
||
public static final String REMOTE = "REMOTE"; | ||
|
||
private static final String LOCAL = "LOCAL"; | ||
|
||
public static final String FTP = "FTP"; | ||
|
||
private static final String HTTP_PREFIX = "http:"; | ||
|
||
private static final String HTTPS_PREFIX = "https:"; | ||
|
||
private static final String LOCAL_PREFIX = "file:"; | ||
|
||
private static final String FTP_PREFIX = "ftp:"; | ||
|
||
private static final String UNKNOWN = "UNKNOWN"; | ||
|
||
/** | ||
* This method check if the given {@param possibleChild} is contained in the specified | ||
* {@param possibleParent}, i.e. it's a sub-path of it. | ||
* | ||
* @param possibleParent | ||
* @param possibleChild | ||
* @return true if sub-path, false otherwise. | ||
*/ | ||
private static boolean isChild(File possibleParent, File possibleChild) { | ||
File parent = possibleChild.getParentFile(); | ||
while (parent != null) { | ||
if (parent.equals(possibleParent)) { | ||
return true; | ||
} | ||
parent = parent.getParentFile(); | ||
} | ||
return false; | ||
} | ||
|
||
private static String[] getAllowedIps() { | ||
return DSpaceServicesFactory.getInstance() | ||
.getConfigurationService() | ||
.getArrayProperty("allowed.ips.import"); | ||
} | ||
|
||
protected DSpaceRunnableHandler handler; | ||
|
||
public ImportFileUtil() {} | ||
|
||
public ImportFileUtil(DSpaceRunnableHandler handler) { | ||
this.handler = handler; | ||
} | ||
|
||
public Optional<InputStream> getInputStream(String path) { | ||
String fileLocationType = getFileLocationTypeByPath(path); | ||
|
||
if (UNKNOWN.equals(fileLocationType)) { | ||
logWarning("File path is of UNKNOWN type: [" + path + "]"); | ||
return Optional.empty(); | ||
} | ||
|
||
return getInputStream(path, fileLocationType); | ||
} | ||
|
||
protected void logWarning(String message) { | ||
log.warn(message); | ||
if (handler != null) { | ||
handler.logWarning(message); | ||
} | ||
} | ||
|
||
private String getFileLocationTypeByPath(String path) { | ||
if (StringUtils.isNotBlank(path)) { | ||
if (path.startsWith(HTTP_PREFIX) || path.startsWith(HTTPS_PREFIX)) { | ||
return REMOTE; | ||
} else if (path.startsWith(LOCAL_PREFIX)) { | ||
return LOCAL; | ||
} else if (path.startsWith(FTP_PREFIX)) { | ||
return FTP; | ||
} else { | ||
return UNKNOWN; | ||
} | ||
} | ||
|
||
return UNKNOWN; | ||
} | ||
|
||
private Optional<InputStream> getInputStream(String path, String fileLocationType) { | ||
try { | ||
switch (fileLocationType) { | ||
case REMOTE: | ||
return getInputStreamOfRemoteFile(path); | ||
case LOCAL: | ||
return getInputStreamOfLocalFile(path); | ||
case FTP: | ||
return getInputStreamOfFtpFile(path); | ||
default: | ||
} | ||
} catch (IOException e) { | ||
logError(e.getMessage()); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private void logError(String message) { | ||
log.error(message); | ||
if (handler != null) { | ||
handler.logError(message); | ||
} | ||
} | ||
|
||
private Optional<InputStream> getInputStreamOfLocalFile(String path) throws IOException { | ||
Path uploadPath = Paths.get( | ||
DSpaceServicesFactory.getInstance() | ||
.getConfigurationService() | ||
.getProperty("uploads.local-folder") | ||
); | ||
File file = uploadPath.resolve(path.replace(LOCAL_PREFIX + "//", "")) | ||
.toFile() | ||
.getCanonicalFile(); | ||
File possibleParent = uploadPath.toFile().getCanonicalFile(); | ||
if (!isChild(possibleParent, file)) { | ||
throw new IOException("Access to the specified file " + path + " is not allowed"); | ||
} | ||
if (!file.exists()) { | ||
throw new IOException("file " + path + " is not found"); | ||
} | ||
return Optional.of(FileUtils.openInputStream(file)); | ||
} | ||
|
||
private Optional<InputStream> getInputStreamOfRemoteFile(String path) throws IOException { | ||
return Optional.of(new URL(path)) | ||
.filter(url -> Set.of(getAllowedIps()).contains(url.getHost())) | ||
.map(this::openStream); | ||
} | ||
|
||
protected InputStream openStream(URL url) { | ||
try { | ||
return url.openStream(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private Optional<InputStream> getInputStreamOfFtpFile(String url) throws IOException { | ||
URL urlObject = new URL(url); | ||
URLConnection urlConnection = urlObject.openConnection(); | ||
return Optional.of(urlConnection.getInputStream()); | ||
} | ||
} |
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
Oops, something went wrong.