-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
178 lines (136 loc) · 4.95 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# utils
import json
from services.user.auth import isLogged
import mimetypes
# Manejo de archivos
from tempfile import mkdtemp
from os import path, environ
# manipular información
from bson.objectid import ObjectId
# Configuración del servidor
from flask import Flask, render_template, request, after_this_request, send_file
# Cloud Storage
from libs import Cloud_Storage
# Librerias de Mongodb
from libs import Mongodb
# Actualización del tiempo
import datetime
# Rutas de la página
from routes import auths, downloads, examples, project, user, statics, fragments, \
api
# cookies
from utils.cookies import compareValues
from io import BytesIO
if(path.exists('./.env')):
# Variables de entorno en modo desarrollo
from dotenv import load_dotenv
load_dotenv()
USER_DB = environ["USER_DB"]
PASSWORD_DB = environ["PASSWORD_DB_KEY"]
mimetypes.add_type('application/javascript', '.js')
app = Flask(__name__)
app.secret_key = environ['SESSION_KEY']
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config['SESSION_PERMANENT'] = True # La cookie no se guarda para siempre
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_COOKIE_SECURE'] = True
app.config['MONGO_URI'] = f'mongodb+srv://{USER_DB}:{PASSWORD_DB}@cluster0.73uuw.mongodb.net/cs50xni?retryWrites=true&w=majority'
mongodb = Mongodb()
mongodb.connect(app)
mongo = mongodb.client
bucket = Cloud_Storage().bucket
IMAGE_MIMETYPE = {'image/png', 'image/jpeg', 'image/jpg'}
INVALID_FILENAME = {':', '*', '/', '"', '?', '>', '|', '<'}
# Es una herramienta sorpresa que nos ayudara más tarde
# def _item_to_value(iterator, item):
# return item
# def list_directories(bucket_name, prefix):
# if prefix and not prefix.endswith('/'):
# prefix += '/'
# extra_params = {
# "projection": "noAcl",
# "prefix": prefix,
# "delimiter": '/'
# }
# path = "/b/" + bucket_name + "/o"
# iterator = page_iterator.HTTPIterator(
# client=client,
# api_request=client._connection.api_request,
# path=path,
# items_key='prefixes',
# item_to_value=_item_to_value,
# extra_params=extra_params,
# )
# return [x for x in iterator]
# directories = list_directories(STORAGE_BUCKET, route)
# Si el nombre tiene un carácter extraño
def change_folder_name(string):
for invalid_name in INVALID_FILENAME:
if string.find(invalid_name) > -1:
string = string.replace(invalid_name, '_')
return string
@app.before_request
def before_request():
app.permanent_session_lifetime = datetime.timedelta(hours=12);
if(not '/static/' in request.path):
@after_this_request
def request_after(resp):
token = request.cookies.get('USER_TOKEN')
get_info = isLogged(token)
if get_info.get('success'):
# print(get_info)
data = get_info.get('info')
if(get_info.get('success')):
return compareValues(resp, data)
return resp
@app.route('/')
def home():
user = {}
validation = isLogged('USER_TOKEN')
if validation.get('success'):
user_info = validation.get('info')
user_id = ObjectId(user_info.get('user_id'))
user = mongo.db.users.find_one({"_id": user_id})
print(user)
return render_template('home.html', user=user)
return render_template('index.html')
auths(app)
downloads(app)
examples(app)
user(app)
project(app)
fragments(app)
statics(app)
api(app)
@app.route('/tools')
def installers():
return render_template("installers/tools.html")
@app.route('/tools/downloadTurboC')
def download_turbo_c():
# get file and send with send_file
file = BytesIO()
blob = bucket.blob('programs/TCS Installer.exe')
blob.download_to_file(file)
file.seek(0)
return send_file(file, mimetype='application/x-msdownload', as_attachment=True, attachment_filename='TurboC.exe')
@app.route('/about-us')
def about():
return render_template("about us/about.html")
@app.route('/google5e97d84d9d35c069.html')
def googleSearchConsole():
return render_template("google5e97d84d9d35c069.html")
@app.errorhandler(404)
def page_not_found(_):
# note that we set the 404 status explicitly
return render_template('404.html'), 404
if __name__ == '__main__':
if environ['FLASK_ENV'] == 'development':
app.run(debug=True)
else:
app.run(debug=False)
# https://stackoverflow.com/questions/37074977/how-to-get-list-of-folders-in-a-given-bucket-using-google-cloud-api :0
# https://stackoverflow.com/questions/583791/is-it-possible-to-generate-and-return-a-zip-file-with-app-engine creara el zip
# https://stackoverflow.com/questions/41865214/how-to-serve-an-image-from-google-cloud-storage-using-python-flask
# https://stackoverflow.com/questions/38658417/create-blob-from-url-in-gae-using-python
# https://cloud.google.com/appengine/docs/flexible/python/using-cloud-storage
# https://cloud.google.com/storage/docs/json_api/v1/objects/copy