1 package org
.asamk
.signal
.manager
;
3 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
5 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
6 import org
.asamk
.signal
.manager
.util
.IOUtils
;
7 import org
.asamk
.signal
.manager
.util
.Utils
;
8 import org
.whispersystems
.signalservice
.api
.util
.StreamDetails
;
9 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
11 import java
.io
.BufferedWriter
;
13 import java
.io
.FileInputStream
;
14 import java
.io
.FileOutputStream
;
15 import java
.io
.IOException
;
16 import java
.io
.OutputStream
;
17 import java
.io
.OutputStreamWriter
;
18 import java
.nio
.charset
.StandardCharsets
;
20 public class StickerPackStore
{
22 private final File stickersPath
;
24 public StickerPackStore(final File stickersPath
) {
25 this.stickersPath
= stickersPath
;
28 public boolean existsStickerPack(StickerPackId stickerPackId
) {
29 return getStickerPackManifestFile(stickerPackId
).exists();
32 public JsonStickerPack
retrieveManifest(StickerPackId stickerPackId
) throws IOException
{
33 try (final var inputStream
= new FileInputStream(getStickerPackManifestFile(stickerPackId
))) {
34 return new ObjectMapper().readValue(inputStream
, JsonStickerPack
.class);
38 public StreamDetails
retrieveSticker(final StickerPackId stickerPackId
, final int stickerId
) throws IOException
{
39 final var stickerFile
= getStickerPackStickerFile(stickerPackId
, stickerId
);
40 if (!stickerFile
.exists()) {
43 return Utils
.createStreamDetailsFromFile(stickerFile
);
46 public void storeManifest(StickerPackId stickerPackId
, JsonStickerPack manifest
) throws IOException
{
47 try (final var output
= new FileOutputStream(getStickerPackManifestFile(stickerPackId
))) {
48 try (var writer
= new BufferedWriter(new OutputStreamWriter(output
, StandardCharsets
.UTF_8
))) {
49 new ObjectMapper().writeValue(writer
, manifest
);
54 public void storeSticker(StickerPackId stickerPackId
, int stickerId
, StickerStorer storer
) throws IOException
{
55 createStickerPackDir(stickerPackId
);
56 try (final var output
= new FileOutputStream(getStickerPackStickerFile(stickerPackId
, stickerId
))) {
61 private File
getStickerPackManifestFile(StickerPackId stickerPackId
) {
62 return new File(getStickerPackPath(stickerPackId
), "manifest.json");
65 private File
getStickerPackStickerFile(StickerPackId stickerPackId
, int stickerId
) {
66 return new File(getStickerPackPath(stickerPackId
), String
.valueOf(stickerId
));
69 private File
getStickerPackPath(StickerPackId stickerPackId
) {
70 return new File(stickersPath
, Hex
.toStringCondensed(stickerPackId
.serialize()));
73 private void createStickerPackDir(StickerPackId stickerPackId
) throws IOException
{
74 IOUtils
.createPrivateDirectories(getStickerPackPath(stickerPackId
));
78 public interface StickerStorer
{
80 void store(OutputStream outputStream
) throws IOException
;