-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
59 lines (53 loc) · 1.97 KB
/
views.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
# -*- coding: UTF-8 -*-
from PIL import Image
import commands
from django.core.files import File
from django.http import HttpResponse
from django.utils.hashcompat import sha_constructor
from grafos.settings import *
import random
import imgur
BATIK_PATH = MEDIA_ROOT + 'batik/batik-rasterizer.jar '
def index(request):
if request.method == 'POST':
tmpName = sha_constructor(str(random.random())).hexdigest()
tipo = request.POST['type']
svg = request.POST['svg']
filename = request.POST['filename']
width = request.POST['width']
if tipo == 'image/png':
typeString = 'image/png'
ext = '.png'
elif tipo == 'image/jpeg':
typeString = 'image/jpg'
ext = '.jpg'
elif tipo == 'application/pdf':
typeString = 'application/pdf'
ext = '.pdf'
else:
typeString = 'image/svg+xml'
ext = '.svg'
else:
return HttpResponse('<h1>GTFO!!</h1>', mimetype='text/html')
outfile = MEDIA_ROOT + tmpName + ext
#return the url not the image
try:
img_url = request.POST['img_url']
except:
img_url = None
try:
f = open('%s%s%s' % (MEDIA_ROOT,tmpName,'.svg'), 'w')
svgObj = File(f)
svgObj.write(svg.encode('utf-8'))
svgObj.close()
string = 'java -jar '+ str(BATIK_PATH) + ' -m ' + str(tipo) +' -d '+ str(outfile) +' -w '+ str(width) + ' ' + str(svgObj.name)
convert = commands.getoutput(string)
salida = open(outfile)
#only url not the image
if img_url:
return HttpResponse(imgur.upload(salida)['upload']['links']['original'])
response = HttpResponse(salida, mimetype=tipo)
response['Content-Disposition'] = 'attachment; filename=grafico'+ext
return response
except Exception:
return HttpResponse(str(Exception))