github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/attachments/pendingpreviews.go (about) 1 package attachments 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/keybase/client/go/chat/globals" 8 "github.com/keybase/client/go/chat/storage" 9 "github.com/keybase/client/go/chat/utils" 10 "github.com/keybase/client/go/encrypteddb" 11 "github.com/keybase/client/go/protocol/chat1" 12 "golang.org/x/net/context" 13 ) 14 15 type PendingPreviews struct { 16 globals.Contextified 17 utils.DebugLabeler 18 } 19 20 func NewPendingPreviews(g *globals.Context) *PendingPreviews { 21 return &PendingPreviews{ 22 Contextified: globals.NewContextified(g), 23 DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "PendingPreviews", false), 24 } 25 } 26 27 func (p *PendingPreviews) getDir() string { 28 return filepath.Join(p.G().GetSharedCacheDir(), "pendingpreviews") 29 } 30 31 func (p *PendingPreviews) getPath(outboxID chat1.OutboxID) string { 32 return filepath.Join(p.getDir(), outboxID.String()+".preview") 33 } 34 35 func (p *PendingPreviews) keyFn() encrypteddb.KeyFn { 36 return func(ctx context.Context) ([32]byte, error) { 37 return storage.GetSecretBoxKey(ctx, p.G().ExternalG()) 38 } 39 } 40 41 func (p *PendingPreviews) Get(ctx context.Context, outboxID chat1.OutboxID) (res Preprocess, err error) { 42 defer p.Trace(ctx, &err, "Get(%s)", outboxID)() 43 44 file := encrypteddb.NewFile(p.G().ExternalG(), p.getPath(outboxID), p.keyFn()) 45 if err := file.Get(ctx, &res); err != nil { 46 return res, err 47 } 48 return res, nil 49 } 50 51 func (p *PendingPreviews) Put(ctx context.Context, outboxID chat1.OutboxID, pre Preprocess) (err error) { 52 defer p.Trace(ctx, &err, "Put(%s)", outboxID)() 53 if err := os.MkdirAll(p.getDir(), os.ModePerm); err != nil { 54 return err 55 } 56 file := encrypteddb.NewFile(p.G().ExternalG(), p.getPath(outboxID), p.keyFn()) 57 return file.Put(ctx, pre) 58 } 59 60 func (p *PendingPreviews) Remove(ctx context.Context, outboxID chat1.OutboxID) { 61 defer p.Trace(ctx, nil, "Remove(%s)", outboxID)() 62 file := encrypteddb.NewFile(p.G().ExternalG(), p.getPath(outboxID), p.keyFn()) 63 if err := file.Remove(ctx); err != nil { 64 p.Debug(ctx, "Remove: failed: %s", err) 65 } 66 }