-
Notifications
You must be signed in to change notification settings - Fork 2
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
e781fda
commit dbcbdb6
Showing
18 changed files
with
826 additions
and
3 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
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
40 changes: 40 additions & 0 deletions
40
...etplace-service/src/main/java/com/axonivy/market/controller/ReleasePreviewController.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,40 @@ | ||
package com.axonivy.market.controller; | ||
|
||
import com.axonivy.market.model.ReleasePreview; | ||
import com.axonivy.market.service.ReleasePreviewService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
import static com.axonivy.market.constants.RequestMappingConstants.RELEASE_PREVIEW; | ||
|
||
@Log4j2 | ||
@RestController | ||
@RequestMapping(RELEASE_PREVIEW) | ||
@Tag(name = "Release Preview Controller", description = "API to extract zip file and return README data.") | ||
@AllArgsConstructor | ||
public class ReleasePreviewController { | ||
|
||
private final ReleasePreviewService previewService; | ||
|
||
@PostMapping | ||
@Operation() | ||
public ResponseEntity<Object> extractZipFile(@RequestParam(value = "file") MultipartFile file) { | ||
String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString(); | ||
ReleasePreview preview = previewService.extract(file, baseUrl); | ||
if (preview == null) { | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
return ResponseEntity.ok(preview); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
marketplace-service/src/main/java/com/axonivy/market/model/ReleasePreview.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,26 @@ | ||
package com.axonivy.market.model; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.Map; | ||
|
||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ReleasePreview { | ||
|
||
@Schema(description = "Product detail description content ", | ||
example = "{ \"de\": \"E-Sign-Konnektor\", \"en\": \"E-sign connector\" }") | ||
private Map<String, String> description; | ||
@Schema(description = "Setup tab content", example = "{ \"de\": \"Setup\", \"en\": \"Setup\" ") | ||
private Map<String, String> setup; | ||
@Schema(description = "Demo tab content", example = "{ \"de\": \"Demo\", \"en\": \"Demo\" ") | ||
private Map<String, String> demo; | ||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
marketplace-service/src/main/java/com/axonivy/market/service/ReleasePreviewService.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,10 @@ | ||
package com.axonivy.market.service; | ||
|
||
import com.axonivy.market.model.ReleasePreview; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public interface ReleasePreviewService { | ||
|
||
ReleasePreview extract(MultipartFile file, String baseUrl); | ||
|
||
} |
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
141 changes: 141 additions & 0 deletions
141
...lace-service/src/main/java/com/axonivy/market/service/impl/ReleasePreviewServiceImpl.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,141 @@ | ||
package com.axonivy.market.service.impl; | ||
|
||
import com.axonivy.market.constants.CommonConstants; | ||
import com.axonivy.market.constants.ReadmeConstants; | ||
import com.axonivy.market.model.ReadmeContentsModel; | ||
import com.axonivy.market.model.ReleasePreview; | ||
import com.axonivy.market.service.ReleasePreviewService; | ||
import com.axonivy.market.util.ProductContentUtils; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
import static com.axonivy.market.constants.DirectoryConstants.PREVIEW_DIR; | ||
import static com.axonivy.market.util.ProductContentUtils.*; | ||
|
||
@Log4j2 | ||
@Service | ||
@AllArgsConstructor | ||
public class ReleasePreviewServiceImpl implements ReleasePreviewService { | ||
|
||
@Override | ||
public ReleasePreview extract(MultipartFile file, String baseUrl) { | ||
unzip(file); | ||
return extractREADME(baseUrl); | ||
} | ||
|
||
private void unzip(MultipartFile file) { | ||
try { | ||
File extractDir = new File(PREVIEW_DIR); | ||
if (extractDir.exists()) { | ||
cleanDirectory(extractDir.toPath()); | ||
} | ||
|
||
try (ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream())) { | ||
ZipEntry entry; | ||
while ((entry = zipInputStream.getNextEntry()) != null) { | ||
File outFile = new File(extractDir, entry.getName()); | ||
if (entry.isDirectory()) { | ||
outFile.mkdirs(); | ||
} else { | ||
new File(outFile.getParent()).mkdirs(); | ||
try (FileOutputStream fos = new FileOutputStream(outFile)) { | ||
byte[] buffer = new byte[1024]; | ||
int length; | ||
while ((length = zipInputStream.read(buffer)) > 0) { | ||
fos.write(buffer, 0, length); | ||
} | ||
} | ||
} | ||
zipInputStream.closeEntry(); | ||
} | ||
} | ||
} catch (IOException e) { | ||
log.error("#unzip An exception occurred when unzip file {} - message {}", file.getName(), e.getMessage()); | ||
} | ||
} | ||
|
||
private ReleasePreview extractREADME(String baseUrl) { | ||
Map<String, Map<String, String>> moduleContents = new HashMap<>(); | ||
try (Stream<Path> readmePathStream = Files.walk(Paths.get(PREVIEW_DIR))) { | ||
List<Path> readmeFiles = readmePathStream.filter(Files::isRegularFile) | ||
.filter(path -> path.getFileName().toString().startsWith(ReadmeConstants.README_FILE_NAME)) | ||
.toList(); | ||
if (readmeFiles.isEmpty()) { | ||
return null; | ||
} | ||
for (Path readmeFile : readmeFiles) { | ||
String readmeContents = Files.readString(readmeFile); | ||
|
||
if (ProductContentUtils.hasImageDirectives(readmeContents)) { | ||
readmeContents = updateImagesWithDownloadUrl(PREVIEW_DIR, readmeContents, baseUrl); | ||
} | ||
|
||
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeContents); | ||
|
||
ProductContentUtils.mappingDescriptionSetupAndDemo(moduleContents, readmeFile.getFileName().toString(), | ||
readmeContentsModel); | ||
} | ||
ReleasePreview preview = new ReleasePreview(); | ||
preview.setDescription(replaceEmptyContentsWithEnContent(moduleContents.get(DESCRIPTION))); | ||
preview.setDemo(replaceEmptyContentsWithEnContent(moduleContents.get(DEMO))); | ||
preview.setSetup(replaceEmptyContentsWithEnContent(moduleContents.get(SETUP))); | ||
return preview; | ||
} catch (IOException e) { | ||
log.error("Cannot get README file's content from folder {}: {}", | ||
com.axonivy.market.constants.DirectoryConstants.PREVIEW_DIR, e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
public String updateImagesWithDownloadUrl(String unzippedFolderPath, | ||
String readmeContents, String baseUrl) { | ||
Map<String, String> imageUrls = new HashMap<>(); | ||
try (Stream<Path> imagePathStream = Files.walk(Paths.get(unzippedFolderPath))) { | ||
List<Path> allImagePaths = imagePathStream.filter(Files::isRegularFile).filter( | ||
path -> path.getFileName().toString().toLowerCase().matches(CommonConstants.IMAGE_EXTENSION)).toList(); | ||
|
||
allImagePaths.stream() | ||
.filter(Objects::nonNull) | ||
.forEach(imagePath -> { | ||
String imageFileName = imagePath.getFileName().toString(); | ||
String imageIdFormat = String.format("%s/api/image/preview/%s", baseUrl, imageFileName); | ||
imageUrls.put(imageFileName, imageIdFormat); | ||
}); | ||
return ProductContentUtils.replaceImageDirWithImageCustomId(imageUrls, readmeContents); | ||
} catch (Exception e) { | ||
log.error("#updateImagesWithDownloadUrl: Error update image url: {}", e.getMessage()); | ||
return null; | ||
} | ||
} | ||
|
||
private static void cleanDirectory(Path directory) { | ||
try { | ||
if (Files.exists(directory)) { | ||
Files.walk(directory) | ||
.sorted(Comparator.reverseOrder()) | ||
.map(Path::toFile) | ||
.forEach(File::delete); | ||
} | ||
Files.createDirectories(directory); | ||
} catch (IOException e) { | ||
log.error("#cleanDirectory: Error managing directory {} : {}", directory.toString(), e.getMessage()); | ||
} | ||
} | ||
} |
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.