Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Support for creating meta only files

Neeraj Gupta344264882621
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index abdd9e58ce..f4b32da85d 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -478,6 +478,7 @@ func main() {
privateAPI.GET("/files/data/preview", fileHandler.GetPreviewURL)
privateAPI.POST("/files", fileHandler.CreateOrUpdate)
+ privateAPI.POST("/files/meta", fileHandler.CreateMetaFile)
privateAPI.POST("/files/copy", fileHandler.CopyFiles)
privateAPI.PUT("/files/update", fileHandler.Update)
privateAPI.POST("/files/trash", fileHandler.Trash)
diff --git a/server/pkg/api/file.go b/server/pkg/api/file.go
index 4ec205d1bb..df57223fd9 100644
--- a/server/pkg/api/file.go
+++ b/server/pkg/api/file.go
@@ -66,6 +66,32 @@ func (h *FileHandler) CreateOrUpdate(c *gin.Context) {
c.JSON(http.StatusOK, response)
}
+// CreateMetaFile creates an entry for a file
+func (h *FileHandler) CreateMetaFile(c *gin.Context) {
+ userID := auth.GetUserID(c.Request.Header)
+ var file ente.File
+ if err := c.ShouldBindJSON(&file); err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ if file.ID != 0 {
+ handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, "fileID can't be set when creating a new file"
+ return
+ }
+ file.UpdationTime = time.Microseconds()
+
+ // get an ente.App from the ?app= query parameter with a default of photos
+ enteApp := auth.GetApp(c)
+ file.OwnerID = userID
+ file.IsDeleted = false
+ resp, err := h.Controller.CreateMetaFile(c, userID, file, c.Request.UserAgent(), enteApp)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ c.JSON(http.StatusOK, resp)
+}
+
// CopyFiles copies files that are owned by another user
func (h *FileHandler) CopyFiles(c *gin.Context) {
var req ente.CopyFileSyncRequest
diff --git a/server/pkg/controller/file_meta.go b/server/pkg/controller/file_meta.go
new file mode 100644
index 0000000000..5f3712eb00
--- /dev/null
+++ b/server/pkg/controller/file_meta.go
@@ -0,0 +1,28 @@
+package controller
+
+import (
+ "github.com/ente-io/museum/ente"
+ "github.com/ente-io/stacktrace"
+ "github.com/gin-gonic/gin"
+)
+
+// CreateMetaFile adds an entry for a file in the respective tables
+func (c *FileController) CreateMetaFile(ctx *gin.Context, userID int64, file ente.File, userAgent string, app ente.App) (*ente.File, error) {
+ collection, collErr := c.CollectionRepo.Get(file.CollectionID)
+ if collErr != nil {
+ return nil, stacktrace.Propagate(collErr, "")
+ }
+ // Verify that user owns the collection.
+ // Warning: Do not remove this check
+ if collection.Owner.ID != userID {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "collection doesn't belong to user")
+ }
+ if collection.IsDeleted {
+ return nil, stacktrace.Propagate(ente.ErrCollectionDeleted, "collection has been deleted")
+ }
+ if file.OwnerID != userID {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "file ownerID doesn't match with userID")
+ }
+ resp, err := c.FileRepo.CreateMetaFile(file, userID, app)
+ return &resp, stacktrace.Propagate(err, "failed to create meta file")
+}
diff --git a/server/pkg/repo/file.go b/server/pkg/repo/file.go
index 50945cf6b1..f91cbea571 100644
--- a/server/pkg/repo/file.go
+++ b/server/pkg/repo/file.go
@@ -126,6 +126,56 @@ func (repo *FileRepository) Create(
return file, usage, stacktrace.Propagate(err, "")
}
+// CreateMetaFile creates an entry in the database for the given file
+func (repo *FileRepository) CreateMetaFile(
+ file ente.File,
+ collectionOwnerID int64,
+ app ente.App,
+) (ente.File, error) {
+ ctx := context.Background()
+ tx, err := repo.DB.BeginTx(ctx, nil)
+ if err != nil {
+ return file, stacktrace.Propagate(err, "")
+ }
+ if file.OwnerID != collectionOwnerID {
+ return file, stacktrace.Propagate(errors.New("both file and collection should belong to same owner"), "")
+ }
+ var fileID int64
+ err = tx.QueryRowContext(ctx, `INSERT INTO files
+ (owner_id, encrypted_metadata,
+ file_decryption_header, thumbnail_decryption_header, metadata_decryption_header,
+ magic_metadata, pub_magic_metadata, info, updation_time)
+ VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING file_id`,
+ file.OwnerID, file.Metadata.EncryptedData, file.File.DecryptionHeader,
+ file.Thumbnail.DecryptionHeader, file.Metadata.DecryptionHeader,
+ file.MagicMetadata, file.PubicMagicMetadata, file.Info,
+ file.UpdationTime).Scan(&fileID)
+ if err != nil {
+ tx.Rollback()
+ return file, stacktrace.Propagate(err, "")
+ }
+ file.ID = fileID
+ _, err = tx.ExecContext(ctx, `INSERT INTO collection_files
+ (collection_id, file_id, encrypted_key, key_decryption_nonce, is_deleted, updation_time, c_owner_id, f_owner_id)
+ VALUES($1, $2, $3, $4, $5, $6, $7, $8)`, file.CollectionID, file.ID,
+ file.EncryptedKey, file.KeyDecryptionNonce, false, file.UpdationTime, file.OwnerID, collectionOwnerID)
+ if err != nil {
+ tx.Rollback()
+ return file, stacktrace.Propagate(err, "")
+ }
+ _, err = tx.ExecContext(ctx, `UPDATE collections SET updation_time = $1
+ WHERE collection_id = $2`, file.UpdationTime, file.CollectionID)
+ if err != nil {
+ tx.Rollback()
+ return file, stacktrace.Propagate(err, "")
+ }
+ err = tx.Commit()
+ if err != nil {
+ return file, stacktrace.Propagate(err, "")
+ }
+ return file, stacktrace.Propagate(err, "")
+}
+
// markAsNeedingReplication inserts new entries in object_copies, setting the
// current hot DC as the source copy.
//