1 package org
.asamk
.signal
.manager
.storage
.stickerPacks
;
3 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
5 import org
.asamk
.signal
.manager
.api
.StickerPackId
;
6 import org
.asamk
.signal
.manager
.storage
.stickerPacks
.JsonStickerPack
;
7 import org
.asamk
.signal
.manager
.util
.IOUtils
;
8 import org
.asamk
.signal
.manager
.util
.Utils
;
9 import org
.whispersystems
.signalservice
.api
.util
.StreamDetails
;
10 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
12 import java
.io
.BufferedWriter
;
14 import java
.io
.FileInputStream
;
15 import java
.io
.FileOutputStream
;
16 import java
.io
.IOException
;
17 import java
.io
.OutputStream
;
18 import java
.io
.OutputStreamWriter
;
19 import java
.nio
.charset
.StandardCharsets
;
21 public class StickerPackStore
{
23 private final File stickersPath
;
25 public StickerPackStore(final File stickersPath
) {
26 this.stickersPath
= stickersPath
;
29 public boolean existsStickerPack(StickerPackId stickerPackId
) {
30 return getStickerPackManifestFile(stickerPackId
).exists();
33 public JsonStickerPack
retrieveManifest(StickerPackId stickerPackId
) throws IOException
{
34 try (final var inputStream
= new FileInputStream(getStickerPackManifestFile(stickerPackId
))) {
35 return new ObjectMapper().readValue(inputStream
, JsonStickerPack
.class);
39 public StreamDetails
retrieveSticker(final StickerPackId stickerPackId
, final int stickerId
) throws IOException
{
40 final var stickerFile
= getStickerPackStickerFile(stickerPackId
, stickerId
);
41 if (!stickerFile
.exists()) {
44 return Utils
.createStreamDetailsFromFile(stickerFile
);
47 public void storeManifest(StickerPackId stickerPackId
, JsonStickerPack manifest
) throws IOException
{
48 try (final var output
= new FileOutputStream(getStickerPackManifestFile(stickerPackId
))) {
49 try (var writer
= new BufferedWriter(new OutputStreamWriter(output
, StandardCharsets
.UTF_8
))) {
50 new ObjectMapper().writeValue(writer
, manifest
);
55 public void storeSticker(StickerPackId stickerPackId
, int stickerId
, StickerStorer storer
) throws IOException
{
56 createStickerPackDir(stickerPackId
);
57 try (final var output
= new FileOutputStream(getStickerPackStickerFile(stickerPackId
, stickerId
))) {
62 private File
getStickerPackManifestFile(StickerPackId stickerPackId
) {
63 return new File(getStickerPackPath(stickerPackId
), "manifest.json");
66 private File
getStickerPackStickerFile(StickerPackId stickerPackId
, int stickerId
) {
67 return new File(getStickerPackPath(stickerPackId
), String
.valueOf(stickerId
));
70 private File
getStickerPackPath(StickerPackId stickerPackId
) {
71 return new File(stickersPath
, Hex
.toStringCondensed(stickerPackId
.serialize()));
74 private void createStickerPackDir(StickerPackId stickerPackId
) throws IOException
{
75 IOUtils
.createPrivateDirectories(getStickerPackPath(stickerPackId
));
79 public interface StickerStorer
{
81 void store(OutputStream outputStream
) throws IOException
;