Skip to content

armgit / ente

Public Imported from GitHub
0

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

Implement all repo method

Neeraj Guptac5d9b2408f34
diff --git a/server/ente/public_file.go b/server/ente/public_file.go
index bce96ac274..e8462eaf9a 100644
--- a/server/ente/public_file.go
+++ b/server/ente/public_file.go
@@ -35,7 +35,6 @@ type PublicFileUrlRow struct {
CreatedAt int64
UpdatedAt int64
}
-
type FileUrl struct {
LinkID string `json:"linkID" binding:"required"`
URL string `json:"url" binding:"required"`
diff --git a/server/pkg/api/file.go b/server/pkg/api/file.go
index 66147224a5..1de2880de6 100644
--- a/server/pkg/api/file.go
+++ b/server/pkg/api/file.go
@@ -24,7 +24,7 @@ import (
// FileHandler exposes request handlers for all encrypted file related requests
type FileHandler struct {
Controller *controller.FileController
- FileUrlCtrl *controller.PublicFileController
+ FileUrlCtrl *controller.PublicFileLinkController
FileCopyCtrl *file_copy.FileCopyController
FileDataCtrl *filedata.Controller
}
diff --git a/server/pkg/controller/public_file.go b/server/pkg/controller/public_file.go
index d4e8d80346..b15d313c93 100644
--- a/server/pkg/controller/public_file.go
+++ b/server/pkg/controller/public_file.go
@@ -11,27 +11,51 @@ import (
"github.com/lithammer/shortuuid/v3"
)
-// PublicFileController controls share collection operations
-type PublicFileController struct {
+// PublicFileLinkController controls share collection operations
+type PublicFileLinkController struct {
FileController *FileController
EmailNotificationCtrl *emailCtrl.EmailNotificationController
PublicCollectionRepo *public.PublicCollectionRepository
- PublicFileRepo *public.PublicFileRepository
+ FileLinkRepo *public.FileLinkRepository
CollectionRepo *repo.CollectionRepository
UserRepo *repo.UserRepository
JwtSecret []byte
}
-func (c *PublicFileController) CreateFileUrl(ctx *gin.Context, req ente.CreateFileUrl) (*ente.FileUrl, error) {
+func (c *PublicFileLinkController) CreateFileUrl(ctx *gin.Context, req ente.CreateFileUrl) (*ente.FileUrl, error) {
actorUserID := auth.GetUserID(ctx.Request.Header)
accessToken := shortuuid.New()[0:AccessTokenLength]
- id, err := c.PublicFileRepo.Insert(ctx, req.FileID, actorUserID, accessToken)
+ _, err := c.FileLinkRepo.Insert(ctx, req.FileID, actorUserID, accessToken)
if err == nil {
- return &ente.FileUrl{
- LinkID: *id,
- FileID: req.FileID,
- OwnerID: actorUserID,
- }, nil
+ row, rowErr := c.FileLinkRepo.GetActiveFileUrlToken(ctx, req.FileID)
+ if rowErr != nil {
+ return nil, stacktrace.Propagate(rowErr, "failed to get active file url token")
+ }
+ return c.mapRowToFileUrl(ctx, row), nil
+ }
+ return nil, stacktrace.Propagate(err, "failed to create public file link")
+}
+
+func (c *PublicFileLinkController) mapRowToFileUrl(ctx *gin.Context, row *ente.PublicFileUrlRow) *ente.FileUrl {
+ app := auth.GetApp(ctx)
+ var url string
+ if app == ente.Locker {
+ url = c.FileLinkRepo.LockerFileLink(row.Token)
+ } else {
+ url = c.FileLinkRepo.PhotoLink(row.Token)
+ }
+ return &ente.FileUrl{
+ LinkID: row.LinkID,
+ FileID: row.FileID,
+ URL: url,
+ OwnerID: row.OwnerID,
+ ValidTill: row.ValidTill,
+ DeviceLimit: row.DeviceLimit,
+ PasswordEnabled: row.PassHash != nil,
+ Nonce: row.Nonce,
+ OpsLimit: row.OpsLimit,
+ MemLimit: row.MemLimit,
+ EnableDownload: row.EnableDownload,
+ CreatedAt: row.CreatedAt,
}
- return nil, stacktrace.NewError("This endpoint is deprecated. Please use CreatePublicCollectionToken instead")
}
diff --git a/server/pkg/repo/public/public_file.go b/server/pkg/repo/public/public_file.go
index 1ffd57c07d..9841e7edc2 100644
--- a/server/pkg/repo/public/public_file.go
+++ b/server/pkg/repo/public/public_file.go
@@ -10,29 +10,37 @@ import (
"github.com/ente-io/stacktrace"
)
-// PublicFileRepository defines the methods for inserting, updating and
+// FileLinkRepository defines the methods for inserting, updating and
// retrieving entities related to public file
-type PublicFileRepository struct {
- DB *sql.DB
- albumHost string
+type FileLinkRepository struct {
+ DB *sql.DB
+ photoHost string
+ lockerHost string
}
-// NewPublicFileRepository ..
-func NewPublicFileRepository(db *sql.DB, albumHost string) *PublicFileRepository {
+// NewFileLinkRepo ..
+func NewFileLinkRepo(db *sql.DB, albumHost string, lockerHost string) *FileLinkRepository {
if albumHost == "" {
albumHost = "https://albums.ente.io"
}
- return &PublicFileRepository{
+ if lockerHost == "" {
+ lockerHost = "https://locker.ente.io"
+ }
+ return &FileLinkRepository{
DB: db,
- albumHost: albumHost,
+ photoHost: albumHost,
}
}
-func (pcr *PublicFileRepository) GetAlbumUrl(token string) string {
- return fmt.Sprintf("%s/?t=%s", pcr.albumHost, token)
+func (pcr *FileLinkRepository) PhotoLink(token string) string {
+ return fmt.Sprintf("%s/?t=%s", pcr.photoHost, token)
+}
+
+func (pcr *FileLinkRepository) LockerFileLink(token string) string {
+ return fmt.Sprintf("%s/?t=%s", pcr.lockerHost, token)
}
-func (pcr *PublicFileRepository) Insert(
+func (pcr *FileLinkRepository) Insert(
ctx context.Context,
fileID int64,
ownerID int64,
@@ -54,39 +62,14 @@ func (pcr *PublicFileRepository) Insert(
return id, nil
}
-func (pcr *PublicFileRepository) DisableSharing(ctx context.Context, fileID int64) error {
- _, err := pcr.DB.ExecContext(ctx, `UPDATE public_file_tokens SET is_disabled = true where
- file_id = $1 and is_disabled = false`, fileID)
- return stacktrace.Propagate(err, "failed to disable sharing")
-}
-
-func convertRowToFileUrl(rows *sql.Rows) ([]ente.FileUrl, error) {
- var fileUrls []ente.FileUrl
- for rows.Next() {
- fileUrl := ente.FileUrl{}
- var nonce *string
- var memLimit, opsLimit *int64
- err := rows.Scan(&fileUrl.LinkID, &fileUrl.OwnerID, &fileUrl.FileID, &fileUrl.ValidTill, &fileUrl.DeviceLimit, &nonce, &memLimit, &opsLimit, &fileUrl.EnableDownload, &fileUrl.CreatedAt)
- if err != nil {
- return nil, stacktrace.Propagate(err, "failed to scan public file url row")
- }
- fileUrl.Nonce = nonce
- fileUrl.MemLimit = memLimit
- fileUrl.OpsLimit = opsLimit
- fileUrls = append(fileUrls, fileUrl)
- }
- return fileUrls, nil
-}
-
// GetActiveFileUrlToken will return ente.PublicCollectionToken for given collection ID
// Note: The token could be expired or deviceLimit is already reached
-func (pcr *PublicFileRepository) GetActiveFileUrlToken(ctx context.Context, collectionID int64) (*ente.PublicFileUrlRow, error) {
+func (pcr *FileLinkRepository) GetActiveFileUrlToken(ctx context.Context, fileID int64) (*ente.PublicFileUrlRow, error) {
row := pcr.DB.QueryRowContext(ctx, `SELECT id, file_id, owner_id, access_token, valid_till, device_limit,
is_disabled, pw_hash, pw_nonce, mem_limit, ops_limit, enable_download FROM
public_file_tokens WHERE file_id = $1 and is_disabled = FALSE`,
- collectionID)
+ fileID)
- //defer rows.Close()
ret := ente.PublicFileUrlRow{}
err := row.Scan(&ret.LinkID, &ret.FileID, ret.OwnerID, &ret.Token, &ret.ValidTill, &ret.DeviceLimit,
&ret.IsDisabled, &ret.PassHash, &ret.Nonce, &ret.MemLimit, &ret.OpsLimit, &ret.EnableDownload)
@@ -96,7 +79,7 @@ func (pcr *PublicFileRepository) GetActiveFileUrlToken(ctx context.Context, coll
return &ret, nil
}
-func (pcr *PublicFileRepository) GetFileUrlRowByToken(ctx context.Context, accessToken string) (*ente.PublicFileUrlRow, error) {
+func (pcr *FileLinkRepository) GetFileUrlRowByToken(ctx context.Context, accessToken string) (*ente.PublicFileUrlRow, error) {
row := pcr.DB.QueryRowContext(ctx,
`SELECT id, file_id, owner_id, is_disabled, valid_till, device_limit, enable_download, pw_hash, pw_nonce, mem_limit, ops_limit
created_at, updated_at
@@ -115,7 +98,7 @@ func (pcr *PublicFileRepository) GetFileUrlRowByToken(ctx context.Context, acces
}
// UpdateLink will update the row for corresponding public file token
-func (pcr *PublicFileRepository) UpdateLink(ctx context.Context, pct ente.PublicFileUrlRow) error {
+func (pcr *FileLinkRepository) UpdateLink(ctx context.Context, pct ente.PublicFileUrlRow) error {
_, err := pcr.DB.ExecContext(ctx, `UPDATE public_file_tokens SET valid_till = $1, device_limit = $2,
pw_hash = $3, pw_nonce = $4, mem_limit = $5, ops_limit = $6, enable_download = $7
where id = $8`,
@@ -123,21 +106,31 @@ func (pcr *PublicFileRepository) UpdateLink(ctx context.Context, pct ente.Public
return stacktrace.Propagate(err, "failed to update public file token")
}
-func (pcr *PublicFileRepository) GetUniqueAccessCount(ctx context.Context, shareId int64) (int64, error) {
- panic("not implemented, refactor & public file")
+func (pcr *FileLinkRepository) GetUniqueAccessCount(ctx context.Context, linkId string) (int64, error) {
+ row := pcr.DB.QueryRowContext(ctx, `SELECT count(*) FROM public_file_tokens_access_history WHERE id = $1`, linkId)
+ var count int64 = 0
+ err := row.Scan(&count)
+ if err != nil {
+ return -1, stacktrace.Propagate(err, "")
+ }
+ return count, nil
}
-func (pcr *PublicFileRepository) RecordAccessHistory(ctx context.Context, shareID int64, ip string, ua string) error {
- panic("not implemented, refactor & public file")
+func (pcr *FileLinkRepository) RecordAccessHistory(ctx context.Context, shareID int64, ip string, ua string) error {
+ _, err := pcr.DB.ExecContext(ctx, `INSERT INTO public_file_tokens_access_history
+ (id, ip, user_agent) VALUES ($1, $2, $3)
+ ON CONFLICT ON CONSTRAINT unique_access_id_ip_ua DO NOTHING;`,
+ shareID, ip, ua)
+ return stacktrace.Propagate(err, "failed to record access history")
}
// AccessedInPast returns true if the given ip, ua agent combination has accessed the url in the past
-func (pcr *PublicFileRepository) AccessedInPast(ctx context.Context, shareID int64, ip string, ua string) (bool, error) {
+func (pcr *FileLinkRepository) AccessedInPast(ctx context.Context, shareID int64, ip string, ua string) (bool, error) {
panic("not implemented, refactor & public file")
}
// CleanupAccessHistory public_file_tokens_access_history where public_collection_tokens is disabled and the last updated time is older than 30 days
-func (pcr *PublicFileRepository) CleanupAccessHistory(ctx context.Context) error {
+func (pcr *FileLinkRepository) CleanupAccessHistory(ctx context.Context) error {
_, err := pcr.DB.ExecContext(ctx, `DELETE FROM public_file_tokens_access_history WHERE id IN (SELECT id FROM public_file_tokens WHERE is_disabled = TRUE AND updated_at < (now_utc_micro_seconds() - (24::BIGINT * 30 * 60 * 60 * 1000 * 1000)))`)
if err != nil {
return stacktrace.Propagate(err, "failed to clean up public file access history")