-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_python_apps.py
More file actions
121 lines (101 loc) · 4.21 KB
/
my_python_apps.py
File metadata and controls
121 lines (101 loc) · 4.21 KB
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
#!flask/bin/python
import os, sys, commands
import traceback
import logging
import time
from crontab import CronTab
from PIL import Image
from flask import Flask, jsonify, request, redirect, url_for, send_from_directory, flash, render_template
from werkzeug import secure_filename
SIZE = (128, 128)
IMAGE_ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_DIR = os.path.join(APP_ROOT, "upload_files")
commands.getoutput("mkdir -p " + UPLOAD_DIR)
#cron job creation
cron = CronTab(user='krishna',tab='True')
cmd = "/bin/rm " + UPLOAD_DIR + "/*"
job = cron.new(cmd, comment='To clear the uploaded files')
job.minute.every(10)
#Starting the APP
my_app = Flask(__name__)
my_app.secret_key = 'krishna'
my_app.config['UPLOAD_DIR'] = UPLOAD_DIR
# create logging
logging.basicConfig(filename='app.log',
formate='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
@my_app.errorhandler(410)
def not_found(error):
return make_response(jsonify({'Error': 'File Not found'}), 404)
@my_app.errorhandler(400)
def wrong_formate(error):
return make_response(jsonify({'Error': 'Wrong input data formate'}), 400)
def allowed_file(f_name):
return '.' in f_name and \
f_name.rsplit('.', 1)[1] in IMAGE_ALLOWED_EXTENSIONS
@my_app.route('/', methods=['GET', 'POST'])
def index():
err_msg = ""
if request.method == 'POST':
if request.form['button'] == "Convert to Thumbnail":
_file = request.files['image_file']
elif request.form['button'] == "Convert to PDF":
_file = request.files['text_file']
if not _file:
err_msg = "No file selected"
flash(err_msg)
return render_template('startup.html', error="error")
f_name = secure_filename(_file.filename)
_file.save(os.path.join(my_app.config['UPLOAD_DIR'], f_name))
if request.form['button'] == "Convert to Thumbnail":
if allowed_file(_file.filename):
return redirect(url_for('convert', file_name=f_name,
conv_type="image"))
else:
err_msg = "File type should be an Image"
elif request.form['button'] == "Convert to PDF":
if ".pdf" not in _file.filename:
return redirect(url_for('convert', file_name=f_name,
conv_type="pdf"))
else:
err_msg = "File type should not be PDF"
flash(err_msg)
return render_template('startup.html', error="error")
@my_app.route('/convert/<file_name>/<conv_type>')
def convert(file_name, conv_type):
infile = os.path.join(UPLOAD_DIR, file_name)
extension = os.path.splitext(infile)[1].strip(".")
try:
if conv_type == "image":
outfile = os.path.splitext(infile)[0] +\
"_thumbnail"+ time.strftime("%y%m%d%H%M%S") +\
"." + extension
image = Image.open(infile)
image.thumbnail(SIZE, Image.ANTIALIAS)
if not extension or extension.lower() in "jpg":
extension = "JPEG"
image.save(outfile, extension.upper())
elif conv_type == "pdf":
outfile = os.path.splitext(infile)[0] +\
time.strftime("%y%m%d%H%M%S") +\
".pdf"
command = "wkhtmltopdf file://" + infile + " " + outfile
logging.info(command)
status, output = commands.getstatusoutput(command)
logging.info(output)
return redirect(url_for('download',
file_name=os.path.basename(outfile)))
except IOError:
if conv_type == "image":
err_msg = "cannot create thumbnail for '%s'\n" % infile
elif conv_type == "pdf":
err_msg = "cannot convert to PDF from '%s'\n" % infile
logging.error(err_msg)
logging.debug(traceback.format_exc())
@my_app.route('/download/<file_name>')
def download(file_name):
return send_from_directory(my_app.config['UPLOAD_DIR'],
file_name, as_attachment=True)
if __name__ == '__main__':
my_app.run(host='0.0.0.0', debug=True)