-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ce05a1
commit 3234f4b
Showing
8 changed files
with
238 additions
and
43 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
32 changes: 32 additions & 0 deletions
32
src/main/java/fr/recia/collabsoft/configuration/beans/StorageProperties.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,32 @@ | ||
/* | ||
* Copyright (C) 2023 GIP-RECIA, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fr.recia.collabsoft.configuration.beans; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class StorageProperties { | ||
|
||
private String location; | ||
|
||
@Override | ||
public String toString() { | ||
return "\"StorageProperties\": {" + | ||
"\n\t\"location\": \"" + location + "\"" + | ||
"\n}"; | ||
} | ||
|
||
} |
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
87 changes: 87 additions & 0 deletions
87
src/main/java/fr/recia/collabsoft/services/storage/ResourceService.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,87 @@ | ||
/* | ||
* Copyright (C) 2023 GIP-RECIA, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fr.recia.collabsoft.services.storage; | ||
|
||
import com.google.common.io.Files; | ||
import fr.recia.collabsoft.configuration.CollabsoftProperties; | ||
import fr.recia.collabsoft.configuration.beans.StorageProperties; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.io.PathResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
@Slf4j | ||
@Service | ||
public class ResourceService { | ||
|
||
private final StorageProperties storageProperties; | ||
|
||
public ResourceService(CollabsoftProperties collabsoftProperties) { | ||
this.storageProperties = collabsoftProperties.getStorage(); | ||
} | ||
|
||
private String getFilePath(Long fileId, String resourceName) { | ||
return storageProperties.getLocation() + File.separator + fileId + File.separator + resourceName; | ||
} | ||
|
||
public Resource getResource(Long fileId, String resourceName) { | ||
return new PathResource(getFilePath(fileId, resourceName)); | ||
} | ||
|
||
public String saveResource(Long fileId, MultipartFile file, String name) { | ||
if (file.isEmpty()) throw new RuntimeException("File can not be empty"); | ||
try { | ||
final String fileExt = Files.getFileExtension(file.getOriginalFilename()).toLowerCase(); | ||
String fileName = name != null && !name.trim().isEmpty() ? name : new SimpleDateFormat("yyyyMMddHHmmss'." + fileExt + "'").format(new Date()); | ||
File inFile = new File(getFilePath(fileId, fileName)); | ||
if (!inFile.getParentFile().exists()) { | ||
boolean error = !inFile.getParentFile().mkdirs(); | ||
if (error) { | ||
log.error("Can't create directory {} to upload file, track error!", inFile.getParentFile().getPath()); | ||
return null; | ||
} | ||
} | ||
|
||
file.transferTo(inFile); | ||
|
||
return fileName; | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
|
||
public boolean deleteResource(Long fileId, String resourceName) { | ||
final String path = getFilePath(fileId, resourceName); | ||
File file = new File(path); | ||
|
||
if (file.exists()) { | ||
final boolean isDeleted = file.delete(); | ||
if (!isDeleted) { | ||
log.error("Tried to delete the file {} failed, track errors!", path); | ||
return false; | ||
} else return true; | ||
} | ||
log.error("Tried to delete the file {} but it doesn't exist, track errors!", path); | ||
return false; | ||
} | ||
|
||
} |
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