Generate thumbnails for folders
This commit is contained in:
@@ -7,6 +7,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
@@ -17,6 +18,7 @@ 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;
|
||||
import sh.rhiobet.lalafin.api.model.FolderInfo;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ThumbnailService {
|
||||
@@ -29,6 +31,8 @@ public class ThumbnailService {
|
||||
if (fileInfo.filename.endsWith(".zip")) {
|
||||
return this.generateThumbnailFromZip(fileInfo, thumbPath);
|
||||
}
|
||||
} else if (fileInfoBase instanceof FolderInfo) {
|
||||
return this.generateThumbnailForFolder((FolderInfo) fileInfoBase, thumbPath);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -68,4 +72,32 @@ public class ThumbnailService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean generateThumbnailForFolder(FolderInfo folderInfo, Path thumbPath) {
|
||||
Path folderPath = Paths.get(fileApiConfiguration.directory()
|
||||
+ URLDecoder.decode(folderInfo.directUrl, StandardCharsets.UTF_8));
|
||||
|
||||
try {
|
||||
Optional<Path> imgPath = Files.list(folderPath).sorted()
|
||||
.filter(p -> !Files.isDirectory(p)
|
||||
&& (p.toString().endsWith(".png") || p.toString().endsWith(".jpg")))
|
||||
.findFirst();
|
||||
if (imgPath.isPresent()) {
|
||||
thumbPath.getParent().toFile().mkdirs();
|
||||
|
||||
ConvertCmd convert = new ConvertCmd();
|
||||
IMOperation op = new IMOperation();
|
||||
op.addImage(imgPath.get().toString());
|
||||
op.resize(200, null);
|
||||
op.addImage(thumbPath.toString());
|
||||
convert.run(op);
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (IOException | InterruptedException | IM4JavaException ignored) {
|
||||
ignored.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user