ohhhchank3 commited on
Commit
97211a7
1 Parent(s): 2b33de7

Upload 4 files

Browse files
Files changed (4) hide show
  1. TOKEN.txt +1 -0
  2. auth_hander.py +32 -0
  3. store_file_dropbox.py +139 -0
  4. token_dropbox.py +18 -0
TOKEN.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ sl.B0IfpZszExRPzGbl7Sr7mpy6tnHvaamM8Z6ubBM762dK5E0eqIFEWe3NCnYb-84PTA685UOIHvsuMU2WRrzu8ScH0cLF_otI4OC3EPgbsu9lh_9yHb09UMUs2aAHx1A1preT_CC-yOkK
auth_hander.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is responsible for signing , encoding , decoding and returning JWTS
2
+ import time
3
+ from typing import Dict
4
+ import jwt
5
+ from decouple import config
6
+
7
+ JWT_SECRET = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
8
+ JWT_ALGORITHM = "HS256"
9
+
10
+
11
+ def token_response(token: str):
12
+ return {
13
+ "access_token": token
14
+ }
15
+
16
+ # function used for signing the JWT string
17
+ def signJWT(user_email: str) -> Dict[str, str]:
18
+ payload = {
19
+ "user_email": user_email,
20
+ "expires": time.time() + 14400
21
+ }
22
+ token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
23
+
24
+ return token_response(token)
25
+
26
+
27
+ def decodeJWT(token: str) -> dict:
28
+ try:
29
+ decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
30
+ return decoded_token if decoded_token["expires"] >= time.time() else None
31
+ except:
32
+ return {}
store_file_dropbox.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dropbox.files
2
+ import os
3
+
4
+ with open("TOKEN.txt","r") as f:
5
+ TOKEN = f.read()
6
+
7
+ dbx=dropbox.Dropbox(TOKEN)
8
+
9
+ def upload_all_local_files():
10
+ for file in os.listdir("local_files"):
11
+ with open(os.path.join("local_files",file),"rb") as f:
12
+ data = f.read()
13
+ dbx.files_upload(data,f"/{file}")
14
+
15
+ def download_all_cloud_files():
16
+ for entry in dbx.files_list_folder("").entries:
17
+ dbx.files_download_to_file(os.path.join("local_files1",entry.name),f"/{entry.name}")
18
+
19
+
20
+ def upload_file_fix(local_path,cloud_path,token):
21
+ try:
22
+ dbx1=dropbox.Dropbox(token)
23
+ with open(local_path, "rb") as f:
24
+ data = f.read()
25
+ dbx1.files_upload(data, cloud_path)
26
+ print(f"Uploaded file '{local_path}' to '{cloud_path}'")
27
+ except dropbox.exceptions.ApiError as e:
28
+ print(f"Error uploading file '{local_path}': {e}")
29
+
30
+
31
+ def upload_file(local_path, cloud_path):
32
+ try:
33
+
34
+ with open(local_path, "rb") as f:
35
+ data = f.read()
36
+ dbx.files_upload(data, cloud_path)
37
+ print(f"Uploaded file '{local_path}' to '{cloud_path}'")
38
+ except dropbox.exceptions.ApiError as e:
39
+ print(f"Error uploading file '{local_path}': {e}")
40
+
41
+ #download_file("demo1.pdf", os.path.join("local_files1", "demo1.pdf"))
42
+
43
+ def search_files_starting_with_j():
44
+ try:
45
+ # List all files in the root folder
46
+ result = dbx.files_list_folder("")
47
+
48
+ # Filter files starting with 'j'
49
+ files_starting_with_j = [entry.name for entry in result.entries if entry.name.startswith("demo")]
50
+
51
+ # Print the list of files starting with 'j'
52
+ print("Files starting with 'demo':")
53
+ for file_name in files_starting_with_j:
54
+ print(file_name)
55
+ except dropbox.exceptions.ApiError as e:
56
+ print(f"Error searching for files: {e}")
57
+
58
+
59
+
60
+ def upload_file(local_path, cloud_path):
61
+ try:
62
+ # Đọc dữ liệu từ file cục bộ
63
+ with open(local_path, "rb") as f:
64
+ data = f.read()
65
+
66
+ # Tải file lên Dropbox
67
+ dbx.files_upload(data, cloud_path)
68
+
69
+ print(f"Uploaded file '{local_path}' to '{cloud_path}'")
70
+ except dropbox.exceptions.ApiError as e:
71
+ print(f"Error uploading file '{local_path}': {e}")
72
+
73
+
74
+ def download_folder(id):
75
+ try:
76
+ # Tạo thư mục cục bộ nếu nó chưa tồn tại
77
+ os.makedirs(f"temp/{id}", exist_ok=True)
78
+ path = f"temp/{id}"
79
+
80
+ # Liệt kê tất cả các tệp trong thư mục trên Dropbox
81
+ result = dbx.files_list_folder(f"/{id}")
82
+
83
+ # Tải về từng tệp
84
+ for entry in result.entries:
85
+ if isinstance(entry, dropbox.files.FileMetadata):
86
+ cloud_file_path = entry.path_display
87
+ file_name = os.path.basename(cloud_file_path)
88
+ local_file_path = os.path.join(path, file_name)
89
+ dbx.files_download_to_file(local_file_path, cloud_file_path)
90
+ print(f"Downloaded file '{file_name}' to '{local_file_path}'")
91
+ except dropbox.exceptions.ApiError as e:
92
+ print(f"Error downloading files: {e}")
93
+ from fastapi import FastAPI, UploadFile, File, HTTPException
94
+
95
+ def download_file_id(file_name, id):
96
+ try:
97
+
98
+ # Tạo thư mục cục bộ nếu nó chưa tồn tại
99
+ local_folder_path = f"./temp/{id}"
100
+ os.makedirs(local_folder_path, exist_ok=True)
101
+
102
+ # Tải file từ Dropbox về thư mục cục bộ
103
+ local_file_path = os.path.join(local_folder_path, file_name)
104
+ with open(local_file_path, "wb") as f:
105
+ metadata, response = dbx.files_download(f"/{id}/{file_name}")
106
+ f.write(response.content)
107
+ print(f"Downloaded file '{file_name}' to '{local_file_path}'")
108
+
109
+ except dropbox.exceptions.ApiError as e:
110
+ print(f"Error downloading file '{file_name}': {e}")
111
+ raise HTTPException(status_code=500, detail="Internal Server Error")
112
+
113
+ def search_and_download_file(start_char, id):
114
+ try:
115
+ # Liệt kê tất cả các tệp trong thư mục trên Dropbox
116
+ result = dbx.files_list_folder(f"/{id}")
117
+
118
+ # Lọc các tệp bắt đầu bằng ký tự cụ thể
119
+ files_starting_with_char = [entry.name for entry in result.entries if entry.name.startswith(start_char)]
120
+
121
+ if len(files_starting_with_char) == 0:
122
+ print(f"No file found starting with '{start_char}' in folder '{id}'")
123
+ return
124
+
125
+ # Chọn tệp đầu tiên
126
+ file_name = files_starting_with_char[0]
127
+
128
+ # Tải file từ Dropbox về thư mục cục bộ
129
+ local_folder_path = f"./temp/{id}"
130
+ os.makedirs(local_folder_path, exist_ok=True)
131
+ local_file_path = os.path.join(local_folder_path, file_name)
132
+ with open(local_file_path, "wb") as f:
133
+ metadata, response = dbx.files_download(f"/{id}/{file_name}")
134
+ f.write(response.content)
135
+ print(f"Downloaded file '{file_name}' to '{local_file_path}'")
136
+
137
+ except dropbox.exceptions.ApiError as e:
138
+ print(f"Error searching or downloading file: {e}")
139
+ raise HTTPException(status_code=500, detail="Internal Server Error")
token_dropbox.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ url = 'https://api.dropboxapi.com/oauth2/token'
4
+ app_key = "m79mo7gg7mh8pez"
5
+ app_secret = "by93yed7as4qlmo"
6
+ access_code = "TEX7wlfjGgUAAAAAAAAANYydTr5XVeay4hFJyk_B2UA"
7
+
8
+ data = {
9
+ 'code': access_code,
10
+ 'grant_type': 'authorization_code'
11
+ }
12
+
13
+ headers = {
14
+ 'Content-Type': 'application/x-www-form-urlencoded'
15
+ }
16
+
17
+ response = requests.post(url, auth=(app_key, app_secret), data=data, headers=headers)
18
+