Coverage Summary for Class: FileMapper (org.kitodo.filemanagement)
Class |
Class, %
|
Method, %
|
Line, %
|
FileMapper |
100%
(1/1)
|
100%
(5/5)
|
94,4%
(17/18)
|
/*
* (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/
package org.kitodo.filemanagement;
import java.net.URI;
import java.nio.file.Paths;
import org.kitodo.config.KitodoConfig;
/**
* Contains functions to turn a relative URI into an absolute one and vice
* versa.
*
* <p>
* The name of this class can be misleading: the mapper contains no mapping and
* no entry is added to or removed from a mapping.
*/
class FileMapper {
/**
* Maps a relative URI an absolute URI below the Kitodo data directory.
*
* @param uri
* relative path
* @return absolute URI path
*/
URI mapUriToKitodoDataDirectoryUri(URI uri) {
String kitodoDataDirectory = KitodoConfig.getKitodoDataDirectory();
if (uri == null) {
return Paths.get(KitodoConfig.getKitodoDataDirectory()).toUri();
} else {
if (!uri.isAbsolute() && !uri.getRawPath().contains(kitodoDataDirectory)) {
return Paths.get(KitodoConfig.getKitodoDataDirectory(), uri.getRawPath()).toUri();
}
}
return uri;
}
URI unmapUriFromKitodoDataDirectoryUri(URI uri) {
return unmapDirectory(uri, KitodoConfig.getKitodoDataDirectory());
}
/**
* Creates a URI relative to {@code directory} if {@code directory} appears
* in the URI string; otherwise the URI is returned unchanged. The relative
* URI is generated by trimming the URI string to the part after
* {@code directory}.
*
* <p>
* The name of this method is misleading: there is no mapping stored
* anywhere, and URI is not unregistered from a mapping. Usually such a
* function is called relativize.
*
* @param uri
* absolute path
* @param directory
* Kitodo data directory
* @return relative URI path
*/
private URI unmapDirectory(URI uri, String directory) {
String path = uri.toString();
directory = encodeDirectory(directory);
if (path.contains(directory)) {
String[] split = path.split(directory);
String shortUri = split[1];
return URI.create(shortUri);
}
return uri;
}
private String encodeDirectory(String directory) {
if (directory.contains("\\")) {
directory = directory.replace("\\", "/");
}
return directory;
}
}