Add thumbnails generator for zip files
This commit is contained in:
@@ -22,6 +22,9 @@ public class FileInfoService {
|
||||
@Inject
|
||||
FileApiConfiguration fileApiConfiguration;
|
||||
|
||||
@Inject
|
||||
ThumbnailService thumbnailService;
|
||||
|
||||
public FileInfoBase getInfo(List<PathSegment> names, FileTokenProvider fileTokenProvider) {
|
||||
String requestedPath = "";
|
||||
String requestedFilename = "";
|
||||
@@ -128,7 +131,8 @@ public class FileInfoService {
|
||||
contentInfo.viewUrl = "/view" + requestedUri + "/" + fileUri + "/0";
|
||||
}
|
||||
}
|
||||
if (thumbPath != null && Files.exists(thumbPath)) {
|
||||
if ((thumbPath != null && Files.exists(thumbPath))
|
||||
|| thumbnailService.generateThumbnail(contentInfo, thumbPath)) {
|
||||
contentInfo.thumbnailUrl =
|
||||
"/file" + requestedUri + "/.thumbnails/" + fileUri + ".jpg";
|
||||
}
|
||||
|
||||
71
src/main/java/sh/rhiobet/lalafin/file/ThumbnailService.java
Normal file
71
src/main/java/sh/rhiobet/lalafin/file/ThumbnailService.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package sh.rhiobet.lalafin.file;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import org.im4java.core.ConvertCmd;
|
||||
import org.im4java.core.IM4JavaException;
|
||||
import org.im4java.core.IMOperation;
|
||||
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration;
|
||||
import sh.rhiobet.lalafin.api.model.FileInfo;
|
||||
import sh.rhiobet.lalafin.api.model.FileInfoBase;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ThumbnailService {
|
||||
@Inject
|
||||
FileApiConfiguration fileApiConfiguration;
|
||||
|
||||
public boolean generateThumbnail(FileInfoBase fileInfoBase, Path thumbPath) {
|
||||
if (fileInfoBase instanceof FileInfo) {
|
||||
FileInfo fileInfo = (FileInfo) fileInfoBase;
|
||||
if (fileInfo.filename.endsWith(".zip")) {
|
||||
return this.generateThumbnailFromZip(fileInfo, thumbPath);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean generateThumbnailFromZip(FileInfo fileInfo, Path thumbPath) {
|
||||
Path zipPath = Paths.get(fileApiConfiguration.directory()
|
||||
+ URLDecoder.decode(fileInfo.directUrl, StandardCharsets.UTF_8));
|
||||
|
||||
Path tmpPath = null;
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(zipPath.toFile());
|
||||
ZipEntry entry = zipFile.stream().filter(e -> !e.isDirectory()).findFirst().get();
|
||||
tmpPath = Paths
|
||||
.get("/tmp/" + fileInfo.filename + "_" + entry.getName().replace("/", "_"));
|
||||
Files.copy(zipFile.getInputStream(entry), tmpPath);
|
||||
thumbPath.getParent().toFile().mkdirs();
|
||||
|
||||
ConvertCmd convert = new ConvertCmd();
|
||||
IMOperation op = new IMOperation();
|
||||
op.addImage(tmpPath.toString());
|
||||
op.resize(200, null);
|
||||
op.addImage(thumbPath.toString());
|
||||
convert.run(op);
|
||||
|
||||
return true;
|
||||
} catch (NoSuchElementException | IOException | InterruptedException | IM4JavaException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (tmpPath != null) {
|
||||
try {
|
||||
Files.delete(tmpPath);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user