-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
530 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS teldrive.file_shares ( | ||
id uuid NOT NULL DEFAULT gen_random_uuid(), | ||
file_id uuid NOT NULL, | ||
password text NULL, | ||
expires_at timestamp NULL, | ||
created_at timestamp NOT NULL DEFAULT timezone('utc'::text, now()), | ||
updated_at timestamp NOT NULL DEFAULT timezone('utc'::text, now()), | ||
user_id bigint NOT NULL, | ||
CONSTRAINT file_shares_pkey PRIMARY KEY (id), | ||
CONSTRAINT fk_file FOREIGN KEY (file_id) REFERENCES teldrive.files (id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE INDEX idx_file_shares_file_id ON teldrive.file_shares USING btree (file_id); | ||
ALTER TABLE teldrive.files DROP COLUMN IF EXISTS starred; | ||
|
||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
DROP INDEX IF EXISTS teldrive.idx_files_starred_updated_at; | ||
CREATE OR REPLACE FUNCTION teldrive.create_directories(u_id bigint, long_path text) | ||
RETURNS SETOF teldrive.files | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
DECLARE | ||
path_parts TEXT[]; | ||
current_directory_id UUID; | ||
new_directory_id UUID; | ||
directory_name TEXT; | ||
path_so_far TEXT; | ||
BEGIN | ||
path_parts := string_to_array(regexp_replace(long_path, '^/+', ''), '/'); | ||
|
||
path_so_far := ''; | ||
|
||
SELECT id INTO current_directory_id | ||
FROM teldrive.files | ||
WHERE parent_id is NULL AND user_id = u_id AND type = 'folder'; | ||
|
||
FOR directory_name IN SELECT unnest(path_parts) LOOP | ||
path_so_far := CONCAT(path_so_far, '/', directory_name); | ||
|
||
SELECT id INTO new_directory_id | ||
FROM teldrive.files | ||
WHERE parent_id = current_directory_id | ||
AND "name" = directory_name | ||
AND "user_id" = u_id; | ||
|
||
IF new_directory_id IS NULL THEN | ||
INSERT INTO teldrive.files ("name", "type", mime_type, parent_id, "user_id") | ||
VALUES (directory_name, 'folder', 'drive/folder', current_directory_id, u_id) | ||
RETURNING id INTO new_directory_id; | ||
END IF; | ||
|
||
current_directory_id := new_directory_id; | ||
END LOOP; | ||
|
||
RETURN QUERY SELECT * FROM teldrive.files WHERE id = current_directory_id; | ||
END; | ||
$function$ | ||
; | ||
-- +goose StatementEnd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/tgdrive/teldrive/pkg/httputil" | ||
"github.com/tgdrive/teldrive/pkg/schemas" | ||
) | ||
|
||
func (sc *Controller) GetShareById(c *gin.Context) { | ||
|
||
res, err := sc.ShareService.GetShareById(c.Param("shareID")) | ||
if err != nil { | ||
httputil.NewError(c, err.Code, err.Error) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, res) | ||
} | ||
|
||
func (sc *Controller) ShareUnlock(c *gin.Context) { | ||
var payload schemas.ShareAccess | ||
if err := c.ShouldBindJSON(&payload); err != nil { | ||
httputil.NewError(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
err := sc.ShareService.ShareUnlock(c.Param("shareID"), &payload) | ||
if err != nil { | ||
httputil.NewError(c, err.Code, err.Error) | ||
return | ||
} | ||
|
||
c.Status(http.StatusOK) | ||
} | ||
|
||
func (sc *Controller) ListShareFiles(c *gin.Context) { | ||
|
||
query := schemas.ShareFileQuery{ | ||
Limit: 500, | ||
Page: 1, | ||
Order: "asc", | ||
Sort: "name", | ||
} | ||
|
||
if err := c.ShouldBindQuery(&query); err != nil { | ||
httputil.NewError(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
res, err := sc.ShareService.ListShareFiles(c.Param("shareID"), &query, c.GetHeader("Authorization")) | ||
if err != nil { | ||
httputil.NewError(c, err.Code, err.Error) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, res) | ||
} | ||
|
||
func (sc *Controller) StreamSharedFile(c *gin.Context) { | ||
sc.ShareService.StreamSharedFile(c, false) | ||
} | ||
|
||
func (sc *Controller) DownloadSharedFile(c *gin.Context) { | ||
sc.ShareService.StreamSharedFile(c, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type FileShare struct { | ||
ID string `gorm:"type:uuid;default:uuid_generate_v4();primary_key"` | ||
FileID string `gorm:"type:uuid;not null"` | ||
Password *string `gorm:"type:text"` | ||
ExpiresAt *time.Time `gorm:"type:timestamp"` | ||
CreatedAt time.Time `gorm:"type:timestamp;not null;default:current_timestamp"` | ||
UpdatedAt time.Time `gorm:"type:timestamp;not null;default:current_timestamp"` | ||
UserID int64 `gorm:"type:bigint;not null"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package schemas | ||
|
||
type ShareAccess struct { | ||
Password string `json:"password" binding:"required"` | ||
} | ||
|
||
type ShareFileQuery struct { | ||
ParentID string `form:"parentId"` | ||
Sort string `form:"sort"` | ||
Order string `form:"order"` | ||
Limit int `form:"limit"` | ||
Page int `form:"page"` | ||
} |
Oops, something went wrong.