+ public String uploadStickerPack(String path) throws IOException, StickerPackInvalidException {
+ SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
+
+ SignalServiceMessageSender messageSender = getMessageSender();
+
+ byte[] packKey = KeyUtils.createStickerUploadKey();
+ String packId = messageSender.uploadStickerManifest(manifest, packKey);
+
+ try {
+ return new URI("https", "signal.art", "/addstickers/", "pack_id=" + URLEncoder.encode(packId, "utf-8") + "&pack_key=" + URLEncoder.encode(Hex.toStringCondensed(packKey), "utf-8"))
+ .toString();
+ } catch (URISyntaxException e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload(final String path) throws IOException, StickerPackInvalidException {
+ ZipFile zip = null;
+ String rootPath = null;
+
+ final File file = new File(path);
+ if (file.getName().endsWith(".zip")) {
+ zip = new ZipFile(file);
+ } else if (file.getName().equals("manifest.json")) {
+ rootPath = file.getParent();
+ } else {
+ throw new StickerPackInvalidException("Could not find manifest.json");
+ }
+
+ JsonStickerPack pack = parseStickerPack(rootPath, zip);
+
+ if (pack.stickers == null) {
+ throw new StickerPackInvalidException("Must set a 'stickers' field.");
+ }
+
+ if (pack.stickers.isEmpty()) {
+ throw new StickerPackInvalidException("Must include stickers.");
+ }
+
+ List<StickerInfo> stickers = new ArrayList<>(pack.stickers.size());
+ for (JsonStickerPack.JsonSticker sticker : pack.stickers) {
+ if (sticker.file == null) {
+ throw new StickerPackInvalidException("Must set a 'file' field on each sticker.");
+ }
+
+ Pair<InputStream, Long> data;
+ try {
+ data = getInputStreamAndLength(rootPath, zip, sticker.file);
+ } catch (IOException ignored) {
+ throw new StickerPackInvalidException("Could not find find " + sticker.file);
+ }
+
+ String contentType = Utils.getFileMimeType(new File(sticker.file), null);
+ StickerInfo stickerInfo = new StickerInfo(data.first(), data.second(), Optional.fromNullable(sticker.emoji).or(""), contentType);
+ stickers.add(stickerInfo);
+ }
+
+ StickerInfo cover = null;
+ if (pack.cover != null) {
+ if (pack.cover.file == null) {
+ throw new StickerPackInvalidException("Must set a 'file' field on the cover.");
+ }
+
+ Pair<InputStream, Long> data;
+ try {
+ data = getInputStreamAndLength(rootPath, zip, pack.cover.file);
+ } catch (IOException ignored) {
+ throw new StickerPackInvalidException("Could not find find " + pack.cover.file);
+ }
+
+ String contentType = Utils.getFileMimeType(new File(pack.cover.file), null);
+ cover = new StickerInfo(data.first(), data.second(), Optional.fromNullable(pack.cover.emoji).or(""), contentType);
+ }
+
+ return new SignalServiceStickerManifestUpload(
+ pack.title,
+ pack.author,
+ cover,
+ stickers);
+ }
+
+ private static JsonStickerPack parseStickerPack(String rootPath, ZipFile zip) throws IOException {
+ InputStream inputStream;
+ if (zip != null) {
+ inputStream = zip.getInputStream(zip.getEntry("manifest.json"));
+ } else {
+ inputStream = new FileInputStream((new File(rootPath, "manifest.json")));
+ }
+ return new ObjectMapper().readValue(inputStream, JsonStickerPack.class);