-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 555d713
Showing
56 changed files
with
10,135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
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,3 @@ | ||
API_ID = 25927942 | ||
API_HASH = "61c4e30931721b62b7585ef45c4415a5" | ||
DB_URL = "mongodb+srv://amrit:[email protected]/pocketDrive" |
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,2 @@ | ||
node_modules | ||
test |
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,22 @@ | ||
import express from "express"; | ||
import bodyParser from "body-parser"; | ||
import morgan from "morgan"; | ||
import { router } from "./routes/routes.js"; | ||
import cors from "cors"; | ||
|
||
const port = process.env.PORT || 3000; | ||
|
||
const app = express(); | ||
app.use(cors()); | ||
app.use(bodyParser.json()); | ||
app.use(morgan("dev")); | ||
|
||
app.use("/v1", router); | ||
|
||
// Handling Errors | ||
app.use((err, req, res, next) => { | ||
console.log(err.stack); | ||
res.status(500).send("Internal server error"); | ||
}); | ||
|
||
app.listen(port, console.log(`Server Listening on ${port}`)); |
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,7 @@ | ||
import { config } from "dotenv"; | ||
config() | ||
|
||
const {API_ID, API_HASH,DB_URL} = process.env | ||
export const apiCred = {apiId:Number(API_ID),apiHash:API_HASH} | ||
export const CONNECTION_RETRIES = 5 | ||
export const dbUri = DB_URL |
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,47 @@ | ||
import { StringSession } from "telegram/sessions/index.js"; | ||
import { CONNECTION_RETRIES, apiCred, } from "../config.js"; | ||
import { TelegramClient, Api } from "telegram"; | ||
import { saveStringSession } from "../model/db.js"; | ||
|
||
export const sendCodeHandler = async (req, res,next) => { | ||
try { | ||
const phoneNumber = req.body.user.phoneNo | ||
const stringSession = new StringSession("") | ||
const teleClient = new TelegramClient(stringSession, apiCred.apiId, apiCred.apiHash, { connectionRetries: CONNECTION_RETRIES, }) | ||
await teleClient.connect() | ||
const { phoneCodeHash, timeout } = await teleClient.invoke(new Api.auth.SendCode({ | ||
...apiCred, | ||
phoneNumber, | ||
settings: new Api.CodeSettings({ | ||
allowFlashcall: true, | ||
currentNumber: true, | ||
allowAppHash: true, | ||
}) | ||
})) | ||
const sess = teleClient.session.save() | ||
await saveStringSession(phoneNumber,sess); | ||
res.json({ phoneCodeHash }) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
export const teleLoginHandler = async (req, res,next) => { | ||
try { | ||
const { phoneCode, phoneCodeHash } = req.body | ||
const phoneNumber = req.body.user.phoneNo | ||
const m = req.body.user.session | ||
console.log(phoneCode, phoneCodeHash, phoneNumber,m) | ||
const stringSession = new StringSession(m) | ||
const teleClient = new TelegramClient(stringSession, apiCred.apiId, apiCred.apiHash, { connectionRetries: CONNECTION_RETRIES, testServers:false}) | ||
await teleClient.connect() | ||
const signIn = await teleClient.invoke(new Api.auth.SignIn({ | ||
phoneNumber, phoneCode, phoneCodeHash | ||
})) | ||
const session = teleClient.session.save() | ||
await saveStringSession(phoneNumber, session) | ||
res.send("Success") | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
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,124 @@ | ||
export const addToBin = async (req, res) => { | ||
try { | ||
const { user, file } = req.body; | ||
|
||
// Find the index of the file in user's files array | ||
const index = user.files.findIndex((x) => x.id === file.id); | ||
|
||
// If file is found, mark it as deleted | ||
if (index !== -1) { | ||
user.files[index].deleted = true; | ||
for (let i = 0; i < user.collections.length; i++) { | ||
if (!user.collections[i]) continue; | ||
const collection = user.collections[i]; | ||
const fileIndex = collection.files.findIndex((x) => x.id === file.id); | ||
if (fileIndex !== -1) { | ||
collection.files[fileIndex].deleted = true; | ||
} | ||
} | ||
await user.save(); | ||
res.json({ message: "Added to Bin" }); | ||
} else { | ||
res.status(404).json({ message: "File not found" }); | ||
} | ||
} catch (error) { | ||
console.error("Error adding to Bin:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
export const restoreFromBin = async (req, res) => { | ||
try { | ||
const { user, file } = req.body; | ||
|
||
// Find the index of the file in user's files array | ||
const index = user.files.findIndex((x) => x.id === file.id); | ||
|
||
// If file is found, mark it as deleted | ||
if (index !== -1) { | ||
user.files[index].deleted = false; | ||
for (let i = 0; i < user.collections.length; i++) { | ||
if (!user.collections[i]) continue; | ||
const collection = user.collections[i]; | ||
const fileIndex = collection.files.findIndex((x) => x.id === file.id); | ||
if (fileIndex !== -1) { | ||
collection.files[fileIndex].deleted = false; | ||
} | ||
} | ||
await user.save(); | ||
res.json({ message: "Restored" }); | ||
} else { | ||
res.status(404).json({ message: "File not found" }); | ||
} | ||
} catch (error) { | ||
console.error("Error adding to Bin:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
export const permanentlyDelete = async (req, res) => { | ||
try { | ||
const { user, file } = req.body; | ||
|
||
// Find the index of the file in user's files array | ||
const index = user.files.findIndex((x) => x.id === file.id); | ||
|
||
// If file is found, mark it as deleted | ||
if (index !== -1) { | ||
user.files.splice(index, 1); | ||
for (let i = 0; i < user.collections.length; i++) { | ||
if (!user.collections[i]) continue; | ||
const collection = user.collections[i]; | ||
const fileIndex = collection.files.findIndex((x) => x.id === file.id); | ||
if (fileIndex !== -1) { | ||
collection.files.splice(fileIndex, 1); | ||
} | ||
} | ||
await user.save(); | ||
res.json({ message: "Deleted" }); | ||
} else { | ||
res.status(404).json({ message: "File not found" }); | ||
} | ||
} catch (error) { | ||
console.error("Error adding to Bin:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
export const emptyBin = async (req, res) => { | ||
try { | ||
const { user } = req.body; | ||
user.files = user.files.filter((x) => !x.deleted); | ||
for (let i = 0; i < user.collections.length; i++) { | ||
if (!user.collections[i]) continue; | ||
const collection = user.collections[i]; | ||
collection.files = collection.files.filter((x) => !x.deleted); | ||
} | ||
await user.save(); | ||
res.json({ message: "Bin emptied" }); | ||
} catch (error) { | ||
console.error("Error emptying Bin:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
export const restoreAll = async (req, res) => { | ||
try { | ||
const { user } = req.body; | ||
user.files.forEach((file) => { | ||
file.deleted = false; | ||
}); | ||
for (let i = 0; i < user.collections.length; i++) { | ||
if (!user.collections[i]) continue; | ||
const collection = user.collections[i]; | ||
collection.files.forEach((file) => { | ||
file.deleted = false; | ||
}); | ||
} | ||
await user.save(); | ||
res.json({ message: "Restored all" }); | ||
} catch (error) { | ||
console.error("Error restoring all:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; |
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,162 @@ | ||
export const getAllFiles = (req, res) => { | ||
const user = req.body.user; | ||
res.json(user.files); | ||
}; | ||
|
||
export const getAllCollections = (req, res) => { | ||
const user = req.body.user; | ||
res.json(user.collections); | ||
}; | ||
|
||
export const createCollection = async (req, res) => { | ||
const { collectionName, user } = req.body; | ||
if ( | ||
user.collections.some((element) => element.collectionName == collectionName) | ||
) { | ||
return res.status(500).json({ message: "Collection Already Exists" }); | ||
} | ||
user.collections.push({ | ||
collectionName: collectionName, | ||
files: [], | ||
}); | ||
await user.save(); | ||
res.json(user.collections); | ||
}; | ||
|
||
export const addToCollection = async (req, res) => { | ||
const { collectionName, user, file } = req.body; | ||
console.log(collectionName); | ||
const collections = user.collections; | ||
collections.forEach((element) => { | ||
if (element.collectionName == collectionName) { | ||
element.files.push(file); | ||
} | ||
}); | ||
// user.collections = collections | ||
console.log(user.collections); | ||
await user.save(); | ||
res.json({ message: "Added Successfully" }); | ||
}; | ||
|
||
export const removeFromCollection = async (req, res) => { | ||
const { collectionName, user, file } = req.body; | ||
const collections = user.collections; | ||
collections.forEach((element) => { | ||
if (element.collectionName == collectionName) { | ||
element.files = element.files.filter((x) => x.id != file.id); | ||
} | ||
}); | ||
await user.save(); | ||
res.json({ message: "Removed Successfully" }); | ||
}; | ||
|
||
export const handleFavourite = async (req, res) => { | ||
try { | ||
const { user, file } = req.body; | ||
const targetFile = user.files.find((x) => x.id == file.id); | ||
if (!targetFile) { | ||
return res.status(404).json({ message: "File not found" }); | ||
} | ||
|
||
targetFile.isFavourite = !targetFile.isFavourite; | ||
|
||
if (targetFile.isFavourite) { | ||
user.collections.forEach((element) => { | ||
if (element.collectionName == "Favourites") { | ||
element.files.push(file); | ||
} | ||
}); | ||
user.collections.forEach((collection) => { | ||
collection.files.forEach((file) => { | ||
if (file.id == targetFile.id) { | ||
file.isFavourite = true; | ||
} | ||
}); | ||
}); | ||
} else { | ||
user.collections.forEach((element) => { | ||
if (element.collectionName == "Favourites") { | ||
element.files = element.files.filter((x) => x.id != file.id); | ||
} | ||
}); | ||
user.collections.forEach((collection) => { | ||
collection.files.forEach((file) => { | ||
if (file.id == targetFile.id) { | ||
file.isFavourite = false; | ||
} | ||
}); | ||
}); | ||
} | ||
await user.save(); | ||
|
||
if (targetFile.isFavourite) { | ||
return res.json({ file: targetFile, message: "Added to Favourites" }); | ||
} else { | ||
return res.json({ file: targetFile, message: "Removed from Favourites" }); | ||
} | ||
} catch (error) { | ||
console.error("Error handling favourite:", error); | ||
res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
}; | ||
|
||
export const removeAllFavourites = async (req, res) => { | ||
try { | ||
const { user } = req.body; | ||
user.files.forEach((file) => { | ||
file.isFavourite = false; | ||
}); | ||
user.collections.forEach((element) => { | ||
if (element.collectionName == "Favourites") { | ||
element.files = []; | ||
} | ||
}); | ||
user.collections.forEach((collection) => { | ||
collection.files.forEach((file) => { | ||
file.isFavourite = false; | ||
}); | ||
}); | ||
await user.save(); | ||
res.json({ message: "Removed all favourites" }); | ||
} catch (error) { | ||
console.error("Error removing all favourites:", error); | ||
res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
}; | ||
|
||
export const renameCollection = async (req, res) => { | ||
try { | ||
const { collectionName, user, newName } = req.body; | ||
const collections = user.collections; | ||
collections.forEach((element) => { | ||
if (element.collectionName == collectionName) { | ||
element.collectionName = newName; | ||
} | ||
}); | ||
await user.save(); | ||
res.json({ message: "Renamed Successfully" }); | ||
} catch (error) { | ||
console.error("Error renaming collection:", error); | ||
res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
}; | ||
|
||
export const renameFile = async (req, res) => { | ||
try { | ||
const { user, file, newName } = req.body; | ||
const targetFile = user.files.find((x) => x.id == file.id); | ||
targetFile.name = newName; | ||
user.collections.forEach((element) => { | ||
element.files.forEach((file) => { | ||
if (file.id == targetFile.id) { | ||
file.name = newName; | ||
} | ||
}); | ||
}); | ||
await user.save(); | ||
res.json({ message: "Renamed Successfully" }); | ||
} catch (error) { | ||
console.error("Error renaming file:", error); | ||
res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
}; |
Oops, something went wrong.