summaryrefslogtreecommitdiff
path: root/imguploader.py
blob: 1b59754d5fc48e800632c81d6a868333721b0232 (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
53
54
#!D:/Programs/python3/python.exe
# -*- coding: utf-8 -*-
import cgi, cgitb
import pymysql
import sys, codecs
import configparser, codecs
import connect
from connect import Connect
import config
from config import Config
import path 
from path import Path
import time
import os 
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.imgpath(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.img(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)