- /**
- * Upload the sticker pack from path.
- *
- * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
- * @return if successful, returns the URL to install the sticker pack in the signal app
- */
- public String uploadStickerPack(String path) throws IOException, StickerPackInvalidException {
- SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
-
- SignalServiceMessageSender messageSender = createMessageSender();
-
- byte[] packKey = KeyUtils.createStickerUploadKey();
- String packId = messageSender.uploadStickerManifest(manifest, packKey);
-
- Sticker sticker = new Sticker(Hex.fromStringCondensed(packId), packKey);
- account.getStickerStore().updateSticker(sticker);
- account.save();
-
- try {
- return new URI("https",
- "signal.art",
- "/addstickers/",
- "pack_id=" + URLEncoder.encode(packId, StandardCharsets.UTF_8) + "&pack_key=" + URLEncoder.encode(
- Hex.toStringCondensed(packKey),
- StandardCharsets.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);
- }