-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadDownload.py
141 lines (122 loc) · 4.05 KB
/
uploadDownload.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
from flask import Blueprint, request, jsonify, send_file, redirect
from werkzeug.utils import secure_filename
from models import db, Paste
from nocache import nocache
import datetime
import pydf
import os
uploadDownload = Blueprint('uploadDownload', __name__)
ALLOWED_EXTENSIONS = set([
'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'ppt', 'doc',
'docx', 'xlsx', 'xls', 'pptx', 'mp3', 'c', 'py', 'js',
'html', 'java', 'svg', 'tiff', 'php', 'cpp', 'mp4'
])
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@uploadDownload.route('/d/upload', methods=['POST'])
def geturl():
url = request.get_json().get('url')
language = request.get_json().get('language')
username = request.get_json().get('username')
foundPaste = Paste.query.filter_by(url=url).first()
if foundPaste is not None:
return jsonify({'success': False, 'status': 'URL already exists'})
file = open('files/' + url + '.txt', 'w')
file.write(request.get_json().get('pasteData'))
file.close()
try:
now = datetime.datetime.now()
newPaste = Paste(
url=url,
date=now.strftime("%d-%m-%Y"),
language=language,
uploadType='code',
username=username
)
db.session.add(newPaste)
db.session.commit()
return jsonify({'success': True})
except Exception as e:
print(e)
return jsonify({'success': False})
@uploadDownload.route('/d/<url>')
@nocache
def downloadAsTxt(url):
try:
path = 'files/' + url + '.txt'
return send_file(path, as_attachment=True)
except Exception as e:
print(e)
return redirect('/error')
@uploadDownload.route('/d/pdf/<url>', methods=['GET'])
@nocache
def handlePdf(url):
if createPdf(url):
return handlePdfDownload(url)
return redirect('/error')
@uploadDownload.route('/d/pdf/<size>/<url>', methods=['GET'])
@nocache
def handlePdfDynamicSize(size, url):
if createPdf(url, size):
return handlePdfDownload(url)
return redirect('/error')
def createPdf(url, size='15'):
filePath = 'files/' + url + '.txt'
try:
file = open(filePath, 'r')
pasteData = file.read()
file.close()
pdfFilePath = 'files/' + url + '.pdf'
pdf = pydf.generate_pdf('<p style="font-size:' +
size+'px">'+pasteData+'</p>')
with open(pdfFilePath, 'wb') as f:
f.write(pdf)
f.close()
return True
except Exception as e:
print(e)
return False
def handlePdfDownload(url):
pdfFilePath = 'files/' + url + '.pdf'
return send_file(pdfFilePath, as_attachment=True)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@uploadDownload.route('/d/uploadfile/', methods=['POST'])
def uploadFile():
f = request.files['file']
if allowed_file(f.filename):
foundPaste = Paste.query.filter_by(url=f.filename).first()
if foundPaste is not None:
return redirect('/error')
try:
now = datetime.datetime.now()
username = request.form['username']
print('username', username)
newPaste = Paste(
url=f.filename,
date=now.strftime("%d-%m-%Y"),
language='none',
uploadType='file',
username=username
)
db.session.add(newPaste)
db.session.commit()
except Exception as e:
print(e)
return redirect('/error')
filename = secure_filename(f.filename)
f.save(os.path.join(APP_ROOT + '/files/')+filename)
return redirect('/')
else:
return redirect('/error')
@uploadDownload.route('/d/file/<url>', methods=['GET'])
def downloadFile(url):
try:
path = 'files/' + url
return send_file(path, as_attachment=True)
except Exception as e:
print(e)
return redirect('/error')
@uploadDownload.errorhandler(413)
def error413(e):
return 'File too large', 413