summaryrefslogtreecommitdiff
path: root/cgi-bin/img.py
blob: 7a7751f8a8aacb135a379c0e69997036fb6b15a0 (plain)
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
#!C:\Python364\python3.exe
# -*- coding: utf-8 -*-
import sys, codecs
import cgi, cgitb
import pymysql
import issuedb
from issuedb import IssueDBFactory
import os 
import path 
from path import Path
import time
sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)

form = cgi.FieldStorage()

fileobj = form['upload']
callback = form.getvalue("CKEditorFuncNum")
fname, fext = os.path.splitext(fileobj.filename)

def randname():
    return int(time.time());

rnd_name = '%s%s' % (randname(), fext)
filepath = Path.img(rnd_name) 

# 检查路径是否存在,不存在则创建
dirname = os.path.dirname(filepath)
url = ""
error = ""
if not os.path.exists(dirname):
    try:
        os.makedirs(dirname)
    except:
        error = 'ERROR_CREATE_DIR'
elif not os.access(dirname, os.W_OK):
    error = 'ERROR_DIR_NOT_WRITEABLE'
if not error:
    open(filepath, 'wb').write(fileobj.file.read())
    #url = url_for('static', filename='%s/%s' % ('upload', rnd_name))
    url = Path.url("imgs/" + rnd_name)

res = """
<html>
<script type="text/javascript">
  window.parent.CKEDITOR.tools.callFunction(%s, '%s', '%s');
</script>
</html>
""" % (callback, url, error)

print("Content-type:text/html\n")
print(res)