1 package org
.asamk
.signal
.manager
.util
;
3 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
5 import org
.asamk
.signal
.manager
.JsonStickerPack
;
6 import org
.asamk
.signal
.manager
.StickerPackInvalidException
;
7 import org
.whispersystems
.libsignal
.util
.Pair
;
8 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
9 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceStickerManifestUpload
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
17 import java
.util
.zip
.ZipEntry
;
18 import java
.util
.zip
.ZipFile
;
20 public class StickerUtils
{
22 public static SignalServiceStickerManifestUpload
getSignalServiceStickerManifestUpload(
24 ) throws IOException
, StickerPackInvalidException
{
26 String rootPath
= null;
28 if (file
.getName().endsWith(".zip")) {
29 zip
= new ZipFile(file
);
30 } else if (file
.getName().equals("manifest.json")) {
31 rootPath
= file
.getParent();
33 throw new StickerPackInvalidException("Could not find manifest.json");
36 JsonStickerPack pack
= parseStickerPack(rootPath
, zip
);
38 if (pack
.stickers
== null) {
39 throw new StickerPackInvalidException("Must set a 'stickers' field.");
42 if (pack
.stickers
.isEmpty()) {
43 throw new StickerPackInvalidException("Must include stickers.");
46 List
<SignalServiceStickerManifestUpload
.StickerInfo
> stickers
= new ArrayList
<>(pack
.stickers
.size());
47 for (JsonStickerPack
.JsonSticker sticker
: pack
.stickers
) {
48 if (sticker
.file
== null) {
49 throw new StickerPackInvalidException("Must set a 'file' field on each sticker.");
52 Pair
<InputStream
, Long
> data
;
54 data
= getInputStreamAndLength(rootPath
, zip
, sticker
.file
);
55 } catch (IOException ignored
) {
56 throw new StickerPackInvalidException("Could not find find " + sticker
.file
);
59 String contentType
= Utils
.getFileMimeType(new File(sticker
.file
), null);
60 SignalServiceStickerManifestUpload
.StickerInfo stickerInfo
= new SignalServiceStickerManifestUpload
.StickerInfo(
63 Optional
.fromNullable(sticker
.emoji
).or(""),
65 stickers
.add(stickerInfo
);
68 SignalServiceStickerManifestUpload
.StickerInfo cover
= null;
69 if (pack
.cover
!= null) {
70 if (pack
.cover
.file
== null) {
71 throw new StickerPackInvalidException("Must set a 'file' field on the cover.");
74 Pair
<InputStream
, Long
> data
;
76 data
= getInputStreamAndLength(rootPath
, zip
, pack
.cover
.file
);
77 } catch (IOException ignored
) {
78 throw new StickerPackInvalidException("Could not find find " + pack
.cover
.file
);
81 String contentType
= Utils
.getFileMimeType(new File(pack
.cover
.file
), null);
82 cover
= new SignalServiceStickerManifestUpload
.StickerInfo(data
.first(),
84 Optional
.fromNullable(pack
.cover
.emoji
).or(""),
88 return new SignalServiceStickerManifestUpload(pack
.title
, pack
.author
, cover
, stickers
);
91 private static JsonStickerPack
parseStickerPack(String rootPath
, ZipFile zip
) throws IOException
{
92 InputStream inputStream
;
94 inputStream
= zip
.getInputStream(zip
.getEntry("manifest.json"));
96 inputStream
= new FileInputStream((new File(rootPath
, "manifest.json")));
98 return new ObjectMapper().readValue(inputStream
, JsonStickerPack
.class);
101 private static Pair
<InputStream
, Long
> getInputStreamAndLength(
102 final String rootPath
, final ZipFile zip
, final String subfile
103 ) throws IOException
{
105 final ZipEntry entry
= zip
.getEntry(subfile
);
106 return new Pair
<>(zip
.getInputStream(entry
), entry
.getSize());
108 final File file
= new File(rootPath
, subfile
);
109 return new Pair
<>(new FileInputStream(file
), file
.length());