Skip to content

Commit

Permalink
change upload retention by env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Nov 5, 2023
1 parent 6686f7d commit 193bcb1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
49 changes: 30 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

Telegram Drive is a powerful utility that enables you to create your own cloud storage service using Telegram as the backend.


[![Discord](https://img.shields.io/discord/1142377485737148479?label=discord&logo=discord&style=flat-square&logoColor=white)](https://discord.gg/J2gVAZnHfP)
[![Discord](https://img.shields.io/discord/1142377485737148479?label=discord&logo=discord&style=flat-square&logoColor=white)](https://discord.gg/J2gVAZnHfP)

**Click on icon to join Discord Server for better support**

Expand Down Expand Up @@ -34,28 +33,31 @@ Telegram Drive is a powerful utility that enables you to create your own cloud s
- **UI:** Based on Material You to create nice looking UI themes.
- **Secure:** Your data is secured using Telegram's robust encryption.
- **Flexible Deployment:** Use Docker Compose or deploy without Docker.

## Demo

![demo](./public/demo.png)

[UI Repo ](https://github.com/divyam234/teldrive-ui)

### Deploy using docker-compose

First clone the repository

```sh
git clone https://github.com/divyam234/teldrive
cd teldrive
touch teldrive.db
```


**Follow Below Steps**

- Create the `teldrive.env` file with your variables and start your container.
- Create the `teldrive.env` file with your variables and start your container.

```sh
docker compose up -d
```

- **Go to http://localhost:8080**
- **Uploads from UI will be slower due to limitations of browser use [Rclone](https://github.com/divyam234/rclone) for faster uploads.Make sure to use Multi Bots mode if you are using uploader.**

Expand All @@ -67,26 +69,25 @@ docker compose up -d

- Download the release binary of Teldrive from the releases section.

- Add same env file as above.

- Add same env file as above.
- Now, run the Teldrive executable binary directly.

## Setting up things

If you're locally or remotely hosting, create a file named `teldrive.env` in the root directory and add all the variables there.
If you're locally or remotely hosting, create a file named `teldrive.env` in the root directory and add all the variables there.
An example of `teldrive.env` file:

```sh
APP_ID=1234
APP_HASH=abc
HTTPS=false
JWT_SECRET=abc
DATABASE_URL=abc
DATABASE_URL=postgres://<db username>:<db password>@<db host>/<db name>

```

> **Warning**
>Default Channel can be selected through UI make sure to set it from account settings on first login.<br>
>Use strong JWT secret instead of pure guessable string.You can use openssl to generate it.<br>
> Default Channel can be selected through UI make sure to set it from account settings on first login.<br>
> Use strong JWT secret instead of pure guessable string.You can use openssl to generate it.<br>
```bash
$ openssl rand -hex 32
Expand All @@ -98,9 +99,10 @@ $ openssl rand -hex 32
> What it multi bots feature and what it does? <br>
> This feature shares the Telegram API requests between other bots to avoid getting floodwaited (A kind of rate limiting that Telegram does in the backend to avoid flooding their servers) and to make the server handle more requests. <br>
To enable multi bots, generate new bot tokens from BotFather and add it through UI on first login.
To enable multi bots, generate new bot tokens from BotFather and add it through UI on first login.

### Mandatory Vars

Before running the bot, you will need to set up the following mandatory variables:

- `APP_ID` : This is the API ID for your Telegram account, which can be obtained from my.telegram.org.
Expand All @@ -112,28 +114,37 @@ Before running the bot, you will need to set up the following mandatory variable
- `DATABASE_URL` : Connection String obtained from Postgres DB (you can use Neon db as free alternative fro postgres)

### Optional Vars

In addition to the mandatory variables, you can also set the following optional variables:
- `HTTPS` : Only needed when frontend is on other domain.

- `HTTPS` : Only needed when frontend is on other domain (Default false).

- `PORT` : Change listen port default is 8080

- `ALLOWED_USERS` : Allow certain telegram usernames including yours to access the app.Enter comma seperated telegram usernames here.Its needed when your instance is on public cloud and you want to restrict other people to access you app.
- `COOKIE_SAME_SITE` : Only needed when frontend is on other domain.

- `LAZY_STREAM_BOTS` : If set to true start Bot session and close immediately when stream or download request is over otherwise run bots forever till server stops.
- `COOKIE_SAME_SITE` : Only needed when frontend is on other domain (Default true).

- `LAZY_STREAM_BOTS` : If set to true start Bot session and close immediately when stream or download request is over otherwise run bots forever till server stops (Default true).

- `BG_BOTS_LIMIT` : If LAZY_STREAM_BOTS is set to false it start atmost BG_BOTS_LIMIT no of bots in background to prevent connection recreation on every request (Default 5).

- `UPLOAD_RETENTION` : No of days to keep incomplete uploads parts in channel afterwards these parts are deleted (Default 15).

- `BG_BOTS_LIMIT` : If LAZY_STREAM_BOTS is set to false it start atmost BG_BOTS_LIMIT no of bots in background to prevent connection recreation on every request(Default is 10).
### For making use of Multi Bots support

> **Warning**
>Bots will be auto added as admin in channel if you set them from UI if it fails somehow add it manually.
> Bots will be auto added as admin in channel if you set them from UI if it fails somehow add it manually.
## FAQ

- How to get Postgres DB url ?
> You can set up a local Postgres instance, but it's not recommended due to backup and data transfer hassles. The recommended approach is to use a free cloud-based Postgres DB like [Neon DB](https://neon.tech/).
> You can set up a local Postgres instance, but it's not recommended due to backup and data transfer hassles. The recommended approach is to use a free cloud-based Postgres DB like [Neon DB](https://neon.tech/).
## Contributing

Feel free to contribute to this project if you have any further ideas.

## Donate

If you like this project small contribution would be appreciated [Paypal](https://paypal.me/redux234).
If you like this project small contribution would be appreciated [Paypal](https://paypal.me/redux234).
4 changes: 3 additions & 1 deletion services/upload.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/divyam234/teldrive/mapper"
"github.com/divyam234/teldrive/schemas"
"github.com/divyam234/teldrive/utils"
"github.com/divyam234/teldrive/utils/tgc"

"github.com/divyam234/teldrive/types"
Expand All @@ -30,8 +31,9 @@ type UploadService struct {
func (us *UploadService) GetUploadFileById(c *gin.Context) (*schemas.UploadOut, *types.AppError) {
uploadId := c.Param("id")
parts := []schemas.UploadPartOut{}
config := utils.GetConfig()
if err := us.Db.Model(&models.Upload{}).Order("part_no").Where("upload_id = ?", uploadId).
Where("created_at >= ?", time.Now().UTC().AddDate(0, 0, -15)).
Where("created_at >= ?", time.Now().UTC().AddDate(0, 0, -config.UploadRetention)).
Find(&parts).Error; err != nil {
return nil, &types.AppError{Error: errors.New("failed to fetch from db"), Code: http.StatusInternalServerError}
}
Expand Down
3 changes: 2 additions & 1 deletion utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type Config struct {
RunMigrations bool `envconfig:"RUN_MIGRATIONS" default:"true"`
Port int `envconfig:"PORT" default:"8080"`
LazyStreamBots bool `envconfig:"LAZY_STREAM_BOTS" default:"true"`
BgBotsLimit int `envconfig:"BG_BOTS_LIMIT" default:"10"`
BgBotsLimit int `envconfig:"BG_BOTS_LIMIT" default:"5"`
UploadRetention int `envconfig:"UPLOAD_RETENTION" default:"15"`
ExecDir string
}

Expand Down
5 changes: 3 additions & 2 deletions utils/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/models"
"github.com/divyam234/teldrive/services"
"github.com/divyam234/teldrive/utils"
"github.com/divyam234/teldrive/utils/tgc"
"github.com/gotd/td/tg"
)
Expand Down Expand Up @@ -175,6 +176,7 @@ func FilesDeleteJob() {
func UploadCleanJob() {
db := database.DB
ctx, cancel := context.WithCancel(context.Background())
config := utils.GetConfig()

defer cancel()

Expand All @@ -183,12 +185,11 @@ func UploadCleanJob() {
Select("JSONB_AGG(jsonb_build_object('id',uploads.id,'partId',uploads.part_id)) as files", "uploads.channel_id", "uploads.user_id", "s.session").
Joins("left join teldrive.users as u on u.user_id = uploads.user_id").
Joins("left join (select * from teldrive.sessions order by created_at desc limit 1) as s on s.user_id = uploads.user_id").
Where("uploads.created_at < ?", time.Now().UTC().AddDate(0, 0, -15)).
Where("uploads.created_at < ?", time.Now().UTC().AddDate(0, 0, -config.UploadRetention)).
Group("uploads.channel_id").Group("uploads.user_id").Group("s.session").
Scan(&upResults).Error; err != nil {
return
}

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

0 comments on commit 193bcb1

Please sign in to comment.