Interface StorageService
- All Known Implementing Classes:
DefaultStorageService
This interface exposes methods required to perform basic tasks involving processing of uploaded
files. It is responsible for validation and pre-processing of files (such as unzipping and URL
creation), but interacts with and relies on the StorageProvider
to abstract away the
details of file storage/management.
This service is also responsible for creating the Folders
and Asset
for
the files it processes.
- Author:
- Chad Harchar (charchar), Samarth Dhruva (samarthd)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttributesToAsset
(Asset asset, ResourceWithMetadata resource) Add attributes to the asset based on the resource.void
addAttributesToAsset
(Asset asset, File file) Add attributes to the asset based on the file.default void
addAttributesToAsset
(Asset asset, org.springframework.core.io.Resource resource) Add attributes to the asset based on the resource.void
removeFile
(String filename) Deprecated, for removal: This API element is subject to removal in a future version.void
removeFile
(String filename, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Remove the provided file from the storage provider.void
unzipAndUploadResourcesAsync
(File sourceZipFile, String fileUploadStateId, Asset assetRequest, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, boolean optimizeImg) Deprecated.void
unzipAndUploadResourcesAsync
(File sourceZipFile, String fileUploadStateId, Asset assetRequest, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, boolean optimizeImg, boolean ignoreFolderName) uploadResource
(org.springframework.web.multipart.MultipartFile file, Asset assetRequest, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, boolean optimizeImg) Uploads a single file, creating anAsset
for it (optionally using details specified inassetRequest
if present).void
validateZipResource
(org.springframework.web.multipart.MultipartFile file) A simple method intended to be used to pre-validate aMultipartFile
received in an API request when expecting a zip file.
-
Method Details
-
uploadResource
Asset uploadResource(org.springframework.web.multipart.MultipartFile file, @Nullable Asset assetRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, boolean optimizeImg) Uploads a single file, creating anAsset
for it (optionally using details specified inassetRequest
if present).Validation includes:
- File size is within the size limits of `spring.servlet.multipart.max-file-size`
- File is of a supported mime type from `broadleaf.asset.storage.whitelisted-mime-type-formats`
- File path and name does not already exist on a asset within the system
- Parameters:
file
- the file to be uploaded to the system.assetRequest
- (optional) asset from which to derive certain attributes when creating the finalAsset
for the file. Currently,Asset.tags
andAsset.locale
are used.contextInfo
- context information around sandboxing and multitenant stateoptimizeImg
- optimizes the image iftrue
- Returns:
- the created asset after the associated resource has been uploaded
- Throws:
StorageException
- if there was an error processing the resourceorg.springframework.web.multipart.MaxUploadSizeExceededException
- if the file is too largeFileUploadBadRequestException
- if a URL already belongs to an assetImageOptimizationException
- Throw when the optimization fails. This could be because a stream to read the image could not be opened, the thread performing the optimization was interrupted, or some other reason.
-
unzipAndUploadResourcesAsync
@Deprecated void unzipAndUploadResourcesAsync(File sourceZipFile, String fileUploadStateId, @Nullable Asset assetRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, boolean optimizeImg) Asynchronously upload the individual contents of a zip file to the storage provider, and create all necessaryFolders
andAssets
in the data store for them. This method updates theFileUploadState
with idfileUploadStateId
with the status of upload.Certain validations are done in a way that, if not satisfied, cause the entire contents of the zip to fail to upload. These include:
- The zip file size itself is within
InternalAssetProperties.getMaxZipFileSize()
- Size for every individual extracted file is within
InternalAssetProperties.getMaxIndividualAssetSize()
- Every individual extracted file is of a supported mime type as checked by
StorageProvider.isWhitelistedMimeType(String)
.
- Parameters:
sourceZipFile
- a temporary zip file whose contents should be unzipped and uploaded to the storage provider. This file itself will be deleted following this operation. If the file passes validation andInternalAssetProperties.isStoreZipFileOnProvider()
is true, then a copy of the zip file will be uploaded to the provider.fileUploadStateId
- the context ID of the baseFileUploadState
for this operation. The results of this upload attempt will be updated on this entity.assetRequest
- (optional) asset from which to derive certain attributes when creating the finalAssets
for the files. Currently,Asset.tags
,Asset.locale
, andAsset.folderId
are used. If supplied,Asset.folderId
is used as the root folder under which all other folders/assets will be created.contextInfo
- request context information around sandbox and multitenant stateoptimizeImg
- whether or not to optimize the images
- The zip file size itself is within
-
unzipAndUploadResourcesAsync
void unzipAndUploadResourcesAsync(File sourceZipFile, String fileUploadStateId, @Nullable Asset assetRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo, boolean optimizeImg, boolean ignoreFolderName) Asynchronously upload the individual contents of a zip file to the storage provider, and create all necessaryFolders
andAssets
in the data store for them. This method updates theFileUploadState
with idfileUploadStateId
with the status of upload.Certain validations are done in a way that, if not satisfied, cause the entire contents of the zip to fail to upload. These include:
- The zip file size itself is within
InternalAssetProperties.getMaxZipFileSize()
- Size for every individual extracted file is within
InternalAssetProperties.getMaxIndividualAssetSize()
- Every individual extracted file is of a supported mime type as checked by
StorageProvider.isWhitelistedMimeType(String)
.
- Parameters:
sourceZipFile
- a temporary zip file whose contents should be unzipped and uploaded to the storage provider. This file itself will be deleted following this operation. If the file passes validation andInternalAssetProperties.isStoreZipFileOnProvider()
is true, then a copy of the zip file will be uploaded to the provider.fileUploadStateId
- the context ID of the baseFileUploadState
for this operation. The results of this upload attempt will be updated on this entity.assetRequest
- (optional) asset from which to derive certain attributes when creating the finalAssets
for the files. Currently,Asset.tags
,Asset.locale
, andAsset.folderId
are used. If supplied,Asset.folderId
is used as the root folder under which all other folders/assets will be created.contextInfo
- request context information around sandbox and multitenant stateoptimizeImg
- whether or not to optimize the imagesignoreFolderName
- whether or not to include the parent folder path in the URI resolution of the files within the zip
- The zip file size itself is within
-
validateZipResource
void validateZipResource(org.springframework.web.multipart.MultipartFile file) A simple method intended to be used to pre-validate aMultipartFile
received in an API request when expecting a zip file.While methods like
unzipAndUploadResourcesAsync(File, String, Asset, ContextInfo, boolean)
do perform certain validations, this method is intended to allow immediate rejection of invalid requests before getting too far in the flow.Default validations:
- File has an `application/zip` mime type.
- File size is within
InternalAssetProperties.getMaxZipFileSize()
- Parameters:
file
- the file to validate- Throws:
org.springframework.web.multipart.MaxUploadSizeExceededException
- if the file is exceeds the maximum upload size allowed.FileUploadBadRequestException
- if thefile
is not an `application/zip`
-
removeFile
Deprecated, for removal: This API element is subject to removal in a future version.Remove the provided file from the storage provider.- Parameters:
filename
- the name of the file to delete on the storage provider- Throws:
StorageException
- if an error is encountered
-
removeFile
void removeFile(String filename, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Remove the provided file from the storage provider.- Parameters:
filename
- the name of the file to delete on the storage providercontextInfo
- context of the request: information around sandbox and multitenant state- Throws:
StorageException
- if an error is encountered
-
addAttributesToAsset
Add attributes to the asset based on the file.- Parameters:
asset
- the asset to add attributes tofile
- the file to use to add attributes to the asset
-
addAttributesToAsset
Add attributes to the asset based on the resource.- Parameters:
asset
- the asset to add attributes toresource
- theResource
to use to add attributes to the asset
-
addAttributesToAsset
Add attributes to the asset based on the resource.- Parameters:
asset
- the asset to add attributes toresource
- theResourceWithMetadata
to use to add attributes to the asset
-
unzipAndUploadResourcesAsync(File, String, Asset, ContextInfo, boolean, boolean)