-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AAS Thumbnail operations #150
Changes from 5 commits
8367fa1
4ed740a
dd08e5b
03cd800
878f387
448c0e1
e197f1e
230dcc0
80d1bc0
d650114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
******************************************************************************/ | ||
package org.eclipse.digitaltwin.basyx.aasrepository.backend; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
@@ -32,6 +34,7 @@ | |
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.Reference; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.Resource; | ||
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; | ||
import org.eclipse.digitaltwin.basyx.aasservice.AasService; | ||
import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; | ||
|
@@ -45,9 +48,10 @@ | |
import org.springframework.data.repository.CrudRepository; | ||
|
||
/** | ||
* Default Implementation for the {@link AasRepository} based on Spring {@link CrudRepository} | ||
* Default Implementation for the {@link AasRepository} based on Spring | ||
* {@link CrudRepository} | ||
* | ||
* @author mateusmolina, despen | ||
* @author mateusmolina, despen, zhangzai | ||
* | ||
*/ | ||
public class CrudAasRepository implements AasRepository { | ||
|
@@ -57,15 +61,15 @@ public class CrudAasRepository implements AasRepository { | |
private AasServiceFactory aasServiceFactory; | ||
|
||
private String aasRepositoryName = null; | ||
|
||
public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory) { | ||
this.aasBackend = aasBackendProvider.getCrudRepository(); | ||
this.aasServiceFactory = aasServiceFactory; | ||
} | ||
|
||
public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { | ||
this(aasBackendProvider, aasServiceFactory); | ||
|
||
this.aasRepositoryName = aasRepositoryName; | ||
} | ||
|
||
|
@@ -175,4 +179,62 @@ public String getName() { | |
return aasRepositoryName == null ? AasRepository.super.getName() : aasRepositoryName; | ||
} | ||
|
||
@Override | ||
public File getThumbnail(String aasId) { | ||
Resource resource = getAssetInformation(aasId).getDefaultThumbnail(); | ||
|
||
ThumbnailHandler.throwIfFileDoesNotExist(aasId, resource); | ||
String filePath = resource.getPath(); | ||
return new File(filePath); | ||
} | ||
|
||
@Override | ||
public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) { | ||
Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail(); | ||
|
||
if (thumbnail != null) { | ||
updateThumbnailFile(aasId, fileName, contentType, inputStream, thumbnail); | ||
return; | ||
} | ||
|
||
String filePath = createFile(aasId, fileName, inputStream); | ||
ThumbnailHandler.setNewThumbnail(this, aasId, contentType, filePath); | ||
} | ||
|
||
private void updateThumbnailFile(String aasId, String fileName, String contentType, InputStream inputStream, Resource thumbnail) { | ||
String path = thumbnail.getPath(); | ||
ThumbnailHandler.deleteExistingFile(path); | ||
String filePath = createFile(aasId, fileName, inputStream); | ||
ThumbnailHandler.updateThumbnail(this, aasId, contentType, filePath); | ||
} | ||
|
||
private String createFile(String aasId, String fileName, InputStream inputStream) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to ThumbnailHandler? |
||
String filePath = ThumbnailHandler.createFilePath(ThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName); | ||
ThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath); | ||
return filePath; | ||
} | ||
|
||
@Override | ||
public void deleteThumbnail(String aasId) { | ||
Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail(); | ||
ThumbnailHandler.throwIfFileDoesNotExist(aasId, thumbnail); | ||
|
||
deleteThumbnailFile(thumbnail); | ||
|
||
updateThumbnailInAssetInformation(aasId); | ||
} | ||
|
||
private void updateThumbnailInAssetInformation(String aasId) { | ||
AssetInformation assetInfor = getAssetInformation(aasId); | ||
assetInfor.getDefaultThumbnail().setContentType(""); | ||
assetInfor.getDefaultThumbnail().setPath(""); | ||
setAssetInformation(aasId, assetInfor); | ||
} | ||
|
||
private void deleteThumbnailFile(Resource thumbnail) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to ThumbnailHandle? |
||
String filePath = thumbnail.getPath(); | ||
java.io.File tmpFile = new java.io.File(filePath); | ||
tmpFile.delete(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2023 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.aasrepository.backend; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.InvalidPathException; | ||
import java.nio.file.Paths; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.Resource; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultResource; | ||
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; | ||
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException; | ||
import org.eclipse.digitaltwin.basyx.core.exceptions.FileHandlingException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ThumbnailHandler { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(ThumbnailHandler.class); | ||
|
||
public static void updateThumbnail(AasRepository aasRepo, String aasId, String contentType, String filePath) { | ||
AssetInformation assetInfor = aasRepo.getAssetInformation(aasId); | ||
assetInfor.getDefaultThumbnail().setContentType(contentType); | ||
assetInfor.getDefaultThumbnail().setPath(filePath); | ||
aasRepo.setAssetInformation(aasId, assetInfor); | ||
} | ||
|
||
public static void setNewThumbnail(AasRepository aasRepo, String aasId, String contentType, String filePath) { | ||
Resource resource = new DefaultResource(); | ||
resource.setContentType(contentType); | ||
resource.setPath(filePath); | ||
AssetInformation assetInfor = aasRepo.getAssetInformation(aasId); | ||
assetInfor.setDefaultThumbnail(resource); | ||
aasRepo.setAssetInformation(aasId, assetInfor); | ||
} | ||
|
||
public static void throwIfFileDoesNotExist(String aasId, Resource resource) { | ||
if (resource == null) | ||
throw new FileDoesNotExistException(aasId); | ||
|
||
String filePath = resource.getPath(); | ||
throwIfFilePathIsNotValid(aasId, filePath); | ||
} | ||
|
||
private static void throwIfFilePathIsNotValid(String aasId, String filePath) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move private methods to bottom of the file |
||
if (filePath.isEmpty()) | ||
throw new FileDoesNotExistException(aasId); | ||
try { | ||
Paths.get(filePath); | ||
} catch (InvalidPathException | NullPointerException ex) { | ||
throw new FileDoesNotExistException(aasId); | ||
} | ||
} | ||
|
||
public static String createFilePath(String tmpDirectory, String aasId, String fileName) { | ||
return tmpDirectory + "/" + aasId + "-" + "Thumbnail" + "-" + fileName; | ||
} | ||
|
||
public static void createFileAtSpecifiedPath(String fileName, InputStream inputStream, String filePath) { | ||
java.io.File targetFile = new java.io.File(filePath); | ||
|
||
try (FileOutputStream outStream = new FileOutputStream(targetFile)) { | ||
IOUtils.copy(inputStream, outStream); | ||
} catch (IOException e) { | ||
throw new FileHandlingException(fileName); | ||
} | ||
} | ||
|
||
public static void deleteExistingFile(String path) { | ||
if (path == null || path.isEmpty()) | ||
return; | ||
|
||
try { | ||
Files.deleteIfExists(Paths.get(path, "")); | ||
} catch (IOException e) { | ||
logger.error("Unable to delete the file having path '{}'", path); | ||
} | ||
} | ||
|
||
public static String getTemporaryDirectoryPath() { | ||
String tempDirectoryPath = ""; | ||
try { | ||
tempDirectoryPath = Files.createTempDirectory("basyx-temp-thumbnail").toAbsolutePath().toString(); | ||
} catch (IOException e) { | ||
logger.error("Unable to create file in the temporary path."); | ||
} | ||
return tempDirectoryPath; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move private methods to the bottom of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, move to thumbnailHandler?