Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/history2git15/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ddvk/rmfakecloud/internal/config"
"github.com/ddvk/rmfakecloud/internal/storage"
"github.com/ddvk/rmfakecloud/internal/storage/fs"
"github.com/ddvk/rmfakecloud/internal/storage/models"
"github.com/ddvk/rmfakecloud/internal/ui/viewmodel"
Expand Down Expand Up @@ -86,14 +87,14 @@ func main() {
cfg.DataDir = path.Dir(path.Dir(userdirectory))

filesystem := fs.NewStorage(cfg)
lbs := filesystem.BlobStorage(userdirectory)
lbs := storage.NewBlobStorer(filesystem, filesystem)

if tail != 0 && len(history) > tail {
history = history[len(history)-tail:]
}

for _, h := range history {
tree, err := h.GetHashTree(lbs)
tree, err := h.GetHashTree(lbs.RemoteStorage(path.Base(userdirectory)))
if err != nil {
log.Fatalf("%s: %s: %s", arg, h.Hash, err.Error())
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/relinkfile15/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"

"github.com/ddvk/rmfakecloud/internal/config"
"github.com/ddvk/rmfakecloud/internal/storage"
"github.com/ddvk/rmfakecloud/internal/storage/fs"
"github.com/ddvk/rmfakecloud/internal/storage/models"
)
Expand All @@ -26,17 +27,17 @@ func main() {
cfg := config.FromEnv()
filesystem := fs.NewStorage(cfg)

lbs := filesystem.BlobStorage(userId)
lbs := storage.NewBlobStorer(filesystem, filesystem)

h := models.RootHistory{Hash: rootHash}
oldtree, err := h.GetHashTree(lbs)
oldtree, err := h.GetHashTree(lbs.RemoteStorage(userId))
if err != nil {
log.Fatalf("%s: %s", h.Hash, err.Error())
}

hash, gen, _ := lbs.GetRootIndex()
hash, gen, _ := lbs.RemoteStorage(userId).GetRootIndex()
h = models.RootHistory{Hash: hash, Generation: gen}
curtree, err := h.GetHashTree(lbs)
curtree, err := h.GetHashTree(lbs.RemoteStorage(userId))

for _, doc := range oldtree.Docs {
concerned := false
Expand All @@ -48,7 +49,7 @@ func main() {
}

if concerned {
err = fs.UpdateTree(curtree, lbs, func(t *models.HashTree) error {
err = storage.UpdateTree(curtree, lbs, userId, func(t *models.HashTree) error {
return t.Add(doc)
})
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type App struct {
docStorer storage.DocumentStorer
userStorer storage.UserStorer
metaStorer storage.MetadataStorer
blobStorer storage.BlobStorage
rootStorer storage.RootStorer
blobStorer *storage.BlobStorer
hub *hub.Hub
codeConnector CodeConnector
hwrClient *hwr.HWRClient
Expand Down Expand Up @@ -110,7 +111,6 @@ func (app *App) Stop() {
}
}


// NewApp constructs an app
func NewApp(cfg *config.Config) App {
debugMode := log.GetLevel() >= log.DebugLevel
Expand Down Expand Up @@ -152,13 +152,16 @@ func NewApp(cfg *config.Config) App {
router.Use(requestLoggerMiddleware())
}

blobStorer := storage.NewBlobStorer(fsStorage, fsStorage)

app := App{
router: router,
cfg: cfg,
docStorer: fsStorage,
userStorer: fsStorage,
metaStorer: fsStorage,
blobStorer: fsStorage,
rootStorer: fsStorage,
blobStorer: blobStorer,
hub: ntfHub,
codeConnector: codeConnector,
hwrClient: &hwr.HWRClient{
Expand All @@ -170,10 +173,10 @@ func NewApp(cfg *config.Config) App {

app.registerRoutes(router)

uiApp := ui.New(cfg, fsStorage, codeConnector, ntfHub, fsStorage, fsStorage)
uiApp := ui.New(cfg, fsStorage, codeConnector, ntfHub, fsStorage, blobStorer)
uiApp.RegisterRoutes(router)

storageapp := fs.NewApp(cfg, fsStorage)
storageapp := storage.NewApp(cfg, fsStorage, fsStorage, fsStorage, fsStorage)
storageapp.RegisterRoutes(router)

return app
Expand Down
41 changes: 13 additions & 28 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/ddvk/rmfakecloud/internal/integrations"
"github.com/ddvk/rmfakecloud/internal/messages"
"github.com/ddvk/rmfakecloud/internal/storage"
"github.com/ddvk/rmfakecloud/internal/storage/fs"
"github.com/ddvk/rmfakecloud/internal/storage/models"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -222,18 +222,16 @@ type metapayload struct {
}

func userID(c *gin.Context) string {
//TODO: suppress the warning
//codeql[go/path-injection]
return c.GetString(userIDKey)
return common.SanitizeUid(c.GetString(userIDKey))
}

func extFromContentType(contentType string) (string, error) {
switch contentType {

case "application/epub+zip":
return storage.EpubFileExt, nil
return models.EpubFileExt, nil
case "application/pdf":
return storage.PdfFileExt, nil
return models.PdfFileExt, nil
}
return "", fmt.Errorf("unsupported content type %s", contentType)
}
Expand Down Expand Up @@ -748,7 +746,7 @@ func (app *App) syncUpdateRootV3(c *gin.Context) {
}

uid := userID(c)
newgeneration, err := app.blobStorer.StoreBlob(uid, RootHash, bytes.NewBufferString(rootv3.Hash), rootv3.Generation)
newgeneration, err := app.rootStorer.UpdateRoot(uid, bytes.NewBufferString(rootv3.Hash), rootv3.Generation)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand Down Expand Up @@ -794,8 +792,8 @@ func crcJSON(c *gin.Context, status int, msg any) {

func (app *App) syncGetRootV3(c *gin.Context) {
uid := userID(c)
reader, generation, _, _, err := app.blobStorer.LoadBlob(uid, RootHash)
if err == fs.ErrorNotFound {
roothash, generation, err := app.rootStorer.GetRootIndex(uid)
if err == storage.ErrorNotFound {
log.Warn("No root file found, assuming this is a new account")
c.JSON(http.StatusNotFound, gin.H{"message": "root not found"})
return
Expand All @@ -807,23 +805,16 @@ func (app *App) syncGetRootV3(c *gin.Context) {
return
}

roothash, err := io.ReadAll(reader)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

c.JSON(http.StatusOK, messages.SyncRootV3Response{
Generation: generation,
Hash: string(roothash),
Hash: roothash,
})
}

func (app *App) syncGetRootV4(c *gin.Context) {
uid := userID(c)
reader, generation, _, _, err := app.blobStorer.LoadBlob(uid, RootHash)
if err == fs.ErrorNotFound {
roothash, generation, err := app.rootStorer.GetRootIndex(uid)
if err == storage.ErrorNotFound {
log.Warn("No root file found, assuming this is a new account")
crcJSON(c, http.StatusOK, messages.SyncRootV4Response{
SchemaVersion: SchemaVersion,
Expand All @@ -837,12 +828,6 @@ func (app *App) syncGetRootV4(c *gin.Context) {
return
}

roothash, err := io.ReadAll(reader)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
crcJSON(c, http.StatusOK, messages.SyncRootV4Response{
Generation: generation,
Hash: string(roothash),
Expand Down Expand Up @@ -883,8 +868,8 @@ func (app *App) blobStorageRead(c *gin.Context) {
uid := userID(c)
blobID := common.ParamS(fileKey, c)

reader, _, size, hash, err := app.blobStorer.LoadBlob(uid, blobID)
if err == fs.ErrorNotFound {
reader, size, hash, err := app.blobStorer.LoadBlob(uid, blobID)
if err == storage.ErrorNotFound {
log.Warn(err)
c.AbortWithStatus(http.StatusNotFound)
return
Expand All @@ -908,7 +893,7 @@ func (app *App) blobStorageWrite(c *gin.Context) {
hash := c.GetHeader(common.GCPHashHeader)
log.Debugf("TODO: check/save etc. write file '%s', hash '%s'", fileName, hash)

_, err := app.blobStorer.StoreBlob(uid, blobID, c.Request.Body, 0)
err := app.blobStorer.StoreBlob(uid, blobID, fileName, hash, c.Request.Body)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand Down
Loading
Loading