Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #443 from WitherTick/dev
Browse files Browse the repository at this point in the history
Change API and several fixes
  • Loading branch information
birdup000 authored Mar 6, 2023
2 parents 5fb6515 + bd71a0d commit 7962b36
Show file tree
Hide file tree
Showing 23 changed files with 203 additions and 69 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,3 @@ We using the monorepo structure with [yarn workspaces](https://classic.yarnpkg.c
[![img](https://user-images.githubusercontent.com/34012548/172031316-60d858c7-8401-42f3-9c73-0b75e80c8292.png)](https://discord.gg/PKNVJwAZnR)

[![img](https://media.discordapp.net/attachments/978783095463501834/984317776544014416/ytbutton.png)](https://youtube.com/channel/UCg9WsNAHdOpo8SyM8JHGuZQ)

2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"serialize-error": "^8.1.0",
"serverless-http": "^3.0.1",
"source-map-support": "^0.5.19",
"teledrive-client": "^2.8.2",
"telegram": "^2.15.5",
"uuid-random": "^1.3.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion api/src/Types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { users } from '@prisma/client'
import { TelegramClient } from 'teledrive-client'
import { TelegramClient } from 'telegram'

declare module 'http' {
interface IncomingMessage {
Expand Down
6 changes: 3 additions & 3 deletions api/src/api/middlewares/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextFunction, Request, Response } from 'express'
import { verify } from 'jsonwebtoken'
import { Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { StringSession } from 'teledrive-client/sessions'
import { Logger, TelegramClient } from 'telegram'
import { LogLevel } from 'telegram/extensions/Logger'
import { StringSession } from 'telegram/sessions'
import { prisma } from '../../model'
import { Redis } from '../../service/Cache'
import { API_JWT_SECRET, CONNECTION_RETRIES, TG_CREDS } from '../../utils/Constant'
Expand Down
6 changes: 3 additions & 3 deletions api/src/api/middlewares/TGClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { StringSession } from 'teledrive-client/sessions'
import { Logger, TelegramClient } from 'telegram'
import { LogLevel } from 'telegram/extensions/Logger'
import { StringSession } from 'telegram/sessions'
import { NextFunction, Request, Response } from 'express'
import { CONNECTION_RETRIES, TG_CREDS } from '../../utils/Constant'

Expand Down
6 changes: 3 additions & 3 deletions api/src/api/middlewares/TGSessionAuth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextFunction, Request, Response } from 'express'
import { verify } from 'jsonwebtoken'
import { Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { StringSession } from 'teledrive-client/sessions'
import { Logger, TelegramClient } from 'telegram'
import { LogLevel } from 'telegram/extensions/Logger'
import { StringSession } from 'telegram/sessions'
import { API_JWT_SECRET, CONNECTION_RETRIES, TG_CREDS } from '../../utils/Constant'

export async function TGSessionAuth(req: Request, _: Response, next: NextFunction): Promise<any> {
Expand Down
10 changes: 5 additions & 5 deletions api/src/api/v1/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { AES } from 'crypto-js'
import { Request, Response } from 'express'
import { sign, verify } from 'jsonwebtoken'
import { serializeError } from 'serialize-error'
import { Api, Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { generateRandomBytes } from 'teledrive-client/Helpers'
import { computeCheck } from 'teledrive-client/Password'
import { StringSession } from 'teledrive-client/sessions'
import { Api, Logger, TelegramClient } from 'telegram'
import { LogLevel } from 'telegram/extensions/Logger'
import { generateRandomBytes } from 'telegram/Helpers'
import { computeCheck } from 'telegram/Password'
import { StringSession } from 'telegram/sessions'
import { prisma } from '../../model'
import { Redis } from '../../service/Cache'
import { API_JWT_SECRET, CONNECTION_RETRIES, COOKIE_AGE, FILES_JWT_SECRET, TG_CREDS } from '../../utils/Constant'
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/v1/Dialogs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Api } from 'teledrive-client'
import { Api } from 'telegram'
import bigInt from 'big-integer'
import { Request, Response } from 'express'
import { Redis } from '../../service/Cache'
Expand Down
139 changes: 119 additions & 20 deletions api/src/api/v1/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Request, Response } from 'express'
import { appendFileSync, createReadStream, existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'fs'
import moment from 'moment'
import multer from 'multer'
import { Api, Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { StringSession } from 'teledrive-client/sessions'
import { Api, Logger, TelegramClient } from 'telegram'
import { LogLevel } from 'telegram/extensions/Logger'
import { StringSession } from 'telegram/sessions'
import { prisma } from '../../model'
import { Redis } from '../../service/Cache'
import { CACHE_FILES_LIMIT, CONNECTION_RETRIES, FILES_JWT_SECRET, TG_CREDS } from '../../utils/Constant'
Expand Down Expand Up @@ -115,6 +115,11 @@ export class Files {
.split(',')
} }
}
if (/\.like$/.test(k)) {
obj = { [k.replace(/\.like$/, '')]: {
startsWith: filters[k].toString()
} }
}
return [...res, obj]
}, []),
...excludeParts === 'true' || excludeParts === '1' ? [
Expand Down Expand Up @@ -153,7 +158,7 @@ export class Files {
take: Number(limit) || 10,
orderBy: buildSort(sort as string)
}),
await prisma.files.count({ where })
await prisma.files.count({ where: whereQuery })
]
}

Expand Down Expand Up @@ -263,13 +268,14 @@ export class Files {
@Endpoint.POST({ middlewares: [Auth] })
public async addFolder(req: Request, res: Response): Promise<any> {
const { file: data } = req.body
const count = data?.name ? null : await prisma.files.count({
const count = await prisma.files.count({
where: {
AND: [
{ type: 'folder' },
{ user_id: req.user.id },
{ name: { startsWith: 'New Folder' } },
{ parent_id: data?.parent_id || null }
{ name: { startsWith: data?.name || 'New Folder' } },
{ parent_id: data?.parent_id || null },
{ link_id: data?.link_id || null }
]
}
})
Expand All @@ -279,11 +285,12 @@ export class Files {

return res.send({ file: await prisma.files.create({
data: {
name: data?.name || `New Folder${count ? ` (${count})` : ''}`,
name: (data?.name || 'New Folder') + `${count ? ` (${count})` : ''}`,
mime_type: 'teledrive/folder',
user_id: req.user.id,
type: 'folder',
uploaded_at: new Date(),
link_id: data?.link_id,
...parent ? {
parent_id: parent.id,
sharing_options: parent.sharing_options,
Expand All @@ -293,6 +300,95 @@ export class Files {
}) })
}

@Endpoint.POST({ middlewares: [Auth] })
public async cloneFile(req: Request, res: Response): Promise<any> {
const { file: body } = req.body
const source = await prisma.files.findUnique({ where: { id: body.key } })
const files = await prisma.files.findMany({
where: {
AND: [
{
name: {
startsWith: body.name.replace(/\.part0*\d+$/, '')
}
},
{
user_id: req.user?.id
},
{
parent_id: source.parent_id
},
]
}
})

delete body.key
let countFiles = 0
for (const file of files) {
const { forward_info: forwardInfo, message_id: messageId, mime_type: mimeType } = file
let peerFrom: Api.InputPeerChannel | Api.InputPeerUser | Api.InputPeerChat
let peerTo: Api.InputPeerChannel | Api.InputPeerUser | Api.InputPeerChat
const [type, peerId, _id, accessHash] = forwardInfo?.split('/') ?? []
if (forwardInfo && forwardInfo.match(/^channel\//gi)) {
if (type === 'channel') {
peerFrom = new Api.InputPeerChannel({
channelId: bigInt(peerId),
accessHash: accessHash ? bigInt(accessHash as string) : null })
} else if (type === 'user') {
peerFrom = new Api.InputPeerUser({
userId: bigInt(peerId),
accessHash: bigInt(accessHash as string) })
} else if (type === 'chat') {
peerFrom = new Api.InputPeerChat({
chatId: bigInt(peerId) })
}
}
if ((req.user.settings as Prisma.JsonObject)?.saved_location) {
const [type, peerId, _, accessHash] = ((req.user.settings as Prisma.JsonObject).saved_location as string).split('/')
if (type === 'channel') {
peerTo = new Api.InputPeerChannel({
channelId: bigInt(peerId),
accessHash: accessHash ? bigInt(accessHash as string) : null })
} else if (type === 'user') {
peerTo = new Api.InputPeerUser({
userId: bigInt(peerId),
accessHash: bigInt(accessHash as string) })
} else if (type === 'chat') {
peerTo = new Api.InputPeerChat({
chatId: bigInt(peerId) })
}
}

const chat = await req.tg.invoke(new Api.messages.ForwardMessages({
fromPeer: peerFrom || 'me',
id: [Number(messageId)],
toPeer: peerTo || 'me',
randomId: [bigInt.randBetween('-1e100', '1e100')],
silent: true,
dropAuthor: true
})) as any

const newForwardInfo = forwardInfo ? `${type}/${peerId}/${chat.updates[0].id.toString()}/${accessHash}` : null
const message = {
size: Number(file.size),
message_id: chat.updates[0].id.toString(),
mime_type: mimeType,
forward_info: newForwardInfo,
uploaded_at: new Date(chat.date * 1000)
}

const response = await prisma.files.create({
data: {
...body,
name: files.length == 1 ? body.name : body.name.replace(/\.part0*\d+$/, '')+`.part${String(countFiles + 1).padStart(3, '0')}`,
...message
}
})
if (countFiles++ == 0)
res.send({ file: response })
}
}

@Endpoint.GET('/:id', { middlewares: [AuthMaybe] })
public async retrieve(req: Request, res: Response): Promise<any> {
const { id } = req.params
Expand Down Expand Up @@ -982,7 +1078,7 @@ export class Files {
name,
message_id: file.id.toString(),
mime_type: mimeType,
size,
size: size.value,
user_id: req.user.id,
uploaded_at: new Date(file.date * 1000),
type,
Expand Down Expand Up @@ -1147,6 +1243,7 @@ export class Files {
// ignore
}

let countFiles = 1
for (const file of files) {
let chat: any
if (file.forward_info && file.forward_info.match(/^channel\//gi)) {
Expand Down Expand Up @@ -1176,7 +1273,7 @@ export class Files {
if (cancel) {
throw { status: 422, body: { error: 'canceled' } }
} else {
console.log(`${chat['messages'][0].id} ${downloaded}/${chat['messages'][0].media.document.size} (${downloaded/Number(chat['messages'][0].media.document.size)})`)
console.log(`${chat['messages'][0].id} ${downloaded}/${chat['messages'][0].media.document.size.value} (${downloaded/Number(totalFileSize)*100+'%'})`)
try {
appendFileSync(filename('process-'), buffer)
} catch (error) {
Expand All @@ -1186,18 +1283,20 @@ export class Files {
}
},
close: () => {
console.log(`${chat['messages'][0].id} ${downloaded}/${chat['messages'][0].media.document.size} (${downloaded/Number(chat['messages'][0].media.document.size)})`, '-end-')
try {
const { size } = statSync(filename('process-'))
if (totalFileSize.gt(bigInt(size))) {
rmSync(filename('process-'))
} else {
renameSync(filename('process-'), filename())
console.log(`${chat['messages'][0].id} ${downloaded}/${chat['messages'][0].media.document.size.value} (${downloaded/Number(totalFileSize)*100+'%'})`, '-end-')
if (countFiles++ >= files.length) {
try {
const { size } = statSync(filename('process-'))
if (totalFileSize.gt(bigInt(size))) {
rmSync(filename('process-'))
} else {
renameSync(filename('process-'), filename())
}
} catch (error) {
// ignore
}
} catch (error) {
// ignore
res.end()
}
res.end()
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/v1/Messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Api } from 'teledrive-client'
import { Api } from 'telegram'
import bigInt from 'big-integer'
import { Request, Response } from 'express'
import { Redis } from '../../service/Cache'
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/v1/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Prisma } from '@prisma/client'
import axios from 'axios'
import { Request, Response } from 'express'
import moment from 'moment'
import { Api } from 'teledrive-client'
import { Api } from 'telegram'
import { prisma } from '../../model'
import { Redis } from '../../service/Cache'
import { buildSort } from '../../utils/FilterQuery'
Expand Down
12 changes: 7 additions & 5 deletions install.docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

set -e

echo "Node Version: $(git --version)"
echo "cURL Version: $(docker -v)"
echo "cURL Version: $(docker-compose -v)"
echo "Node Version: $(node -v)"
echo "cURL Version: $(curl --version | head -n 1)"
echo "Docker Version: $(docker -v)"
echo "Docker Compose Version: $(docker-compose -v)"

if [ ! -f docker/.env ]
then
Expand All @@ -31,9 +32,10 @@ then
echo "DB_PASSWORD=$DB_PASSWORD" >> docker/.env

cd docker
docker-compose build teledrive
docker-compose up -d
sleep 2
docker-compose up -d
docker-compose exec teledrive yarn workspace api prisma migrate deploy
else
git reset --hard
git clean -f
Expand All @@ -46,6 +48,6 @@ else
docker-compose up --build --force-recreate -d
sleep 2
docker-compose up -d

docker-compose exec teledrive yarn workspace api prisma migrate deploy
docker image prune -f
fi
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"streamsaver": "^2.0.5",
"sw-precache-cra": "^1.0.0",
"swr": "^1.0.0",
"teledrive-client": "^2.8.2",
"telegram": "^2.15.5",
"through2": "^4.0.2",
"typescript": "^4.0.3",
"use-debounce": "^7.0.1",
Expand Down
6 changes: 3 additions & 3 deletions web/src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import OtpInput from 'react-otp-input'
import QRCode from 'react-qr-code'
import { useHistory } from 'react-router'
import useSWRImmutable from 'swr/immutable'
import { Api } from 'teledrive-client'
import { generateRandomBytes } from 'teledrive-client/Helpers'
import { computeCheck } from 'teledrive-client/Password'
import { Api } from 'telegram'
import { generateRandomBytes } from 'telegram/Helpers'
import { computeCheck } from 'telegram/Password'
import en from 'world_countries_lists/data/countries/en/world.json'
import { fetcher, req } from '../utils/Fetcher'
import { anonymousTelegramClient, telegramClient } from '../utils/Telegram'
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import React, { useEffect, useState } from 'react'
import { useThemeSwitcher } from 'react-css-theme-switcher'
import { useHistory } from 'react-router-dom'
import useSWR from 'swr'
import { Api } from 'teledrive-client'
import { Api } from 'telegram'
import * as serviceWorkerRegistration from '../serviceWorkerRegistration'
import { VERSION } from '../utils/Constant'
import { apiUrl, fetcher, req } from '../utils/Fetcher'
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/dashboard/components/TableFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const TableFiles: React.FC<Props> = ({
const [popup, setPopup] = useState<{ visible: boolean, x?: number, y?: number, row?: any }>()
const [showDetails, setShowDetails] = useState<any>()
const { data: user } = useSWR(showDetails ? `/users/${showDetails.user_id}` : null, fetcher)
const { data: filesParts } = useSWR(showDetails ? `/files?name.match=${encodeURIComponent('\.part0*[0-9]+$')}&name.like=${showDetails.name.replace(/\.part0*\d+$/, '')}%&user_id=${showDetails.user_id}&parent_id${showDetails.parent_id ? `=${showDetails.parent_id}` : '.is=null'}${tab === 'shared' ? '&shared=1' : ''}` : null, fetcher)
const { data: filesParts } = useSWR(showDetails ? `/files?name.like=${showDetails.name.replace(/\.part0*\d+$/, '')}&user_id=${showDetails.user_id}&parent_id${showDetails.parent_id ? `=${showDetails.parent_id}` : '=null'}${tab === 'shared' ? '&shared=1' : ''}` : null, fetcher)
const pasteEnabled = useRef<boolean | null>(null)

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/dashboard/components/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CloudUploadOutlined } from '@ant-design/icons'
import { notification, Typography, Upload as BaseUpload } from 'antd'
import mime from 'mime-types'
import React, { useEffect, useRef } from 'react'
import { Api } from 'teledrive-client'
import { Api } from 'telegram'
import { CHUNK_SIZE, MAX_UPLOAD_SIZE, RETRY_COUNT } from '../../../utils/Constant'
import { req } from '../../../utils/Fetcher'
import { telegramClient } from '../../../utils/Telegram'
Expand Down
Loading

0 comments on commit 7962b36

Please sign in to comment.