Skip to content

Commit

Permalink
upload changes
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Oct 31, 2023
1 parent b2e8f5e commit 7c3e478
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 9 deletions.
2 changes: 2 additions & 0 deletions database/migrations/20230911113332_alter_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

ALTER TABLE teldrive.files DROP CONSTRAINT IF EXISTS unique_file;

ALTER TABLE teldrive.users DROP COLUMN settings;

CREATE UNIQUE INDEX unique_file ON teldrive.files (name, parent_id, user_id) WHERE (status= 'active');
3 changes: 0 additions & 3 deletions database/migrations/20230916012407_alter_user.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ begin
path_parts := string_to_array(regexp_replace(long_path, '^/+', ''), '/');

path_so_far := '';

depth_dir := 0;

SELECT id into current_directory_id FROM teldrive.files WHERE parent_id='root';
SELECT id into current_directory_id FROM teldrive.files WHERE parent_id='root' AND user_id=tg_id;


FOR directory_name IN SELECT unnest(path_parts) LOOP
Expand Down
3 changes: 3 additions & 0 deletions database/migrations/20231031223444_alter_uploads.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- +goose Up

ALTER TABLE teldrive.uploads ADD COLUMN user_id BIGINT;
99 changes: 99 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func main() {

scheduler.Every(1).Hour().Do(cron.FilesDeleteJob)

scheduler.Every(12).Hour().Do(cron.UploadCleanJob)

scheduler.StartAsync()

router.Use(cors.New(cors.Config{
Expand Down
1 change: 1 addition & 0 deletions models/upload.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type Upload struct {
ID string `gorm:"type:text;primary_key;default:generate_uid(16)"`
UploadId string `gorm:"type:text"`
UserId string `gorm:"type:bigint"`
Name string `gorm:"type:text"`
PartNo int `gorm:"type:integer"`
TotalParts int `gorm:"type:integer"`
Expand Down
9 changes: 4 additions & 5 deletions services/upload.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/divyam234/teldrive/mapper"
"github.com/divyam234/teldrive/schemas"
Expand All @@ -29,7 +30,9 @@ type UploadService struct {
func (us *UploadService) GetUploadFileById(c *gin.Context) (*schemas.UploadOut, *types.AppError) {
uploadId := c.Param("id")
parts := []schemas.UploadPartOut{}
if err := us.Db.Model(&models.Upload{}).Order("part_no").Where("upload_id = ?", uploadId).Find(&parts).Error; err != nil {
if err := us.Db.Model(&models.Upload{}).Order("part_no").Where("upload_id = ?", uploadId).
Where("created_at >= ?", time.Now().UTC().AddDate(0, 0, -15)).
Find(&parts).Error; err != nil {
return nil, &types.AppError{Error: errors.New("failed to fetch from db"), Code: http.StatusInternalServerError}
}

Expand Down Expand Up @@ -75,10 +78,6 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty

fileSize := c.Request.ContentLength

if fileSize == 0 {
return nil, &types.AppError{Error: errors.New("filesize must be greater than zero"), Code: http.StatusBadRequest}
}

fileName := uploadQuery.Filename

var msgId int
Expand Down
88 changes: 88 additions & 0 deletions utils/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"encoding/json"
"strconv"
"time"

"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/models"
Expand All @@ -30,13 +31,37 @@ func (a *Files) Scan(value interface{}) error {
return nil
}

type UpFiles []UpFile
type UpFile struct {
ID string `json:"id"`
PartID int `json:"partId"`
}

func (a UpFiles) Value() (driver.Value, error) {
return json.Marshal(a)
}

func (a *UpFiles) Scan(value interface{}) error {
if err := json.Unmarshal(value.([]byte), &a); err != nil {
return err
}
return nil
}

type Result struct {
Files Files
TgSession string
UserId int64
ChannelId int64
}

type UploadResult struct {
Files UpFiles
TgSession string
UserId int64
ChannelId int64
}

func deleteTGMessages(ctx context.Context, result Result) error {

db := database.DB
Expand Down Expand Up @@ -82,6 +107,48 @@ func deleteTGMessages(ctx context.Context, result Result) error {
return nil
}

func cleanUploadsMessages(ctx context.Context, result UploadResult) error {

db := database.DB

client, err := tgc.UserLogin(result.TgSession)

if err != nil {
return err
}

ids := []int{}

fileIds := []string{}

for _, file := range result.Files {
fileIds = append(fileIds, file.ID)
ids = append(ids, file.PartID)

}
err = tgc.RunWithAuth(ctx, client, "", func(ctx context.Context) error {

channel, err := services.GetChannelById(ctx, client, result.ChannelId, strconv.FormatInt(result.UserId, 10))

if err != nil {
return err
}

messageDeleteRequest := tg.ChannelsDeleteMessagesRequest{Channel: channel, ID: ids}

_, err = client.API().ChannelsDeleteMessages(ctx, &messageDeleteRequest)
if err != nil {
return err
}
return nil
})
if err == nil {
db.Where("id = any($1)", fileIds).Delete(&models.Upload{})
}

return nil
}

func FilesDeleteJob() {
db := database.DB
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -103,3 +170,24 @@ func FilesDeleteJob() {
deleteTGMessages(ctx, row)
}
}

func UploadCleanJob() {
db := database.DB
ctx, cancel := context.WithCancel(context.Background())

defer cancel()

var upResults []UploadResult
if err := db.Model(&models.Upload{}).
Select("JSONB_AGG(jsonb_build_object('id',uploads.id,'partId',uploads.part_id)) as files", "uploads.channel_id", "uploads.user_id", "u.tg_session").
Joins("left join teldrive.users as u on u.user_id = uploads.user_id").
Where("uploads.created_at < ?", time.Now().UTC().AddDate(0, 0, -15)).
Group("uploads.channel_id").Group("uploads.user_id").Group("u.tg_session").
Scan(&upResults).Error; err != nil {
return
}

for _, row := range upResults {
cleanUploadsMessages(ctx, row)
}
}

0 comments on commit 7c3e478

Please sign in to comment.