backend for xcvr appview

refactor caching

+32 -23
+3 -23
server/internal/handler/lrcHandlers.go
··· 247 247 if cid == "" { 248 248 h.serverError(w, errors.New("empty cid")) 249 249 } 250 - uploadDir := "./uploads" 251 - _, err = os.Stat(uploadDir) 252 - if os.IsNotExist(err) { 253 - os.Mkdir(uploadDir, 0755) 254 - } 255 - 256 - imgPath := fmt.Sprintf("%s/%s%s", uploadDir, did, cid) 257 - _, err = os.Stat(imgPath) 250 + imgPath, err := h.rm.AddImageToCache(did, cid, r.Context()) 258 251 if err != nil { 259 - blob, err := atputils.SyncGetBlob(did, cid, r.Context()) 260 - if err != nil { 261 - h.serverError(w, err) 262 - return 263 - } 264 - file, err := os.Create(imgPath) 265 - if err != nil { 266 - h.serverError(w, err) 267 - return 268 - } 269 - _, err = file.Write(blob) 270 - if err != nil { 271 - h.serverError(w, err) 272 - return 273 - } 252 + h.serverError(w, errors.New("beep: "+err.Error())) 253 + return 274 254 } 275 255 276 256 stats, err := os.Stat(imgPath)
+29
server/internal/recordmanager/media.go
··· 3 3 import ( 4 4 "context" 5 5 "errors" 6 + "fmt" 6 7 atoauth "github.com/bluesky-social/indigo/atproto/auth/oauth" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" 8 9 lexutil "github.com/bluesky-social/indigo/lex/util" 9 10 "mime/multipart" 11 + "os" 12 + "rvcx/internal/atputils" 10 13 "rvcx/internal/lex" 11 14 "rvcx/internal/oauth" 12 15 "rvcx/internal/types" ··· 15 18 16 19 func (rm *RecordManager) PostImage(cs *atoauth.ClientSession, file multipart.File, fileHeader *multipart.FileHeader, ctx context.Context) (*lexutil.BlobSchema, error) { 17 20 return oauth.UploadBLOB(cs, file, fileHeader, ctx) 21 + } 22 + 23 + func (rm *RecordManager) AddImageToCache(did string, cid string, ctx context.Context) (string, error) { 24 + uploadDir := "./uploads" 25 + _, err := os.Stat(uploadDir) 26 + if os.IsNotExist(err) { 27 + os.Mkdir(uploadDir, 0755) 28 + } 29 + 30 + imgPath := fmt.Sprintf("%s/%s%s", uploadDir, did, cid) 31 + _, err = os.Stat(imgPath) 32 + if err != nil { 33 + blob, err := atputils.SyncGetBlob(did, cid, ctx) 34 + if err != nil { 35 + return "", err 36 + } 37 + file, err := os.Create(imgPath) 38 + if err != nil { 39 + return "", err 40 + } 41 + _, err = file.Write(blob) 42 + if err != nil { 43 + return "", err 44 + } 45 + } 46 + return imgPath, nil 18 47 } 19 48 20 49 func (rm *RecordManager) PostMedia(cs *atoauth.ClientSession, mr *types.ParseMediaRequest, ctx context.Context) error {