Kindeditor+web.py+SAE Storage 实现文件上传,kindeditorweb.py,#上传图片文件class
文章由Byrx.net分享于2019-03-23 09:03:49
Kindeditor+web.py+SAE Storage 实现文件上传,kindeditorweb.py,#上传图片文件class
#上传图片文件class UploadImage: def POST(self): x = web.input(imgFile={}) if 'SERVER_SOFTWARE' in os.environ: #in SAE from sae.storage import Bucket bucket = Bucket(Config.StorageName) if 'imgFile' in x: filepath = x.imgFile.filename.replace('\\\\','/') filename = filepath.split('/')[-1] fileType = filename[filename.index('.')+1:].lower() #生成随机文件文件名 newFileName = self.createNewFileName(fileType) bucket.put_object(newFileName, x.imgFile.file) imageURL = bucket.generate_url(newFileName) return json.dumps({ 'error': 0, 'url': imageURL}) else: pass def createNewFileName(self, fileType, randomlength=8): str = '' chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789' length = len(chars) - 1 random = Random() for i in range(randomlength): str+=chars[random.randint(0, length)] return '%s.%s' % (str, fileType)#远程服务器文件管理器class FileManager: def GET(self): if 'SERVER_SOFTWARE' in os.environ: #in SAE from sae.storage import Bucket bucket = Bucket(Config.StorageName) fileList = [] for sFile in bucket.list(): #需要判断是否是照片,以及文件类型 fileType = sFile.name[sFile.name.index('.')+1:].lower() isPhoto = fileType in Config.imageFileTypes file = {'filename':sFile.name, 'datetime':sFile.last_modified, 'filesize':sFile.bytes, 'is_photo':isPhoto, 'filetype':fileType} fileList.append(file) return json.dumps({'current_url':'http://fighterlog-fighterlogstorage.stor.sinaapp.com/', 'total_count':len(fileList), 'file_list':fileList}) else: #in local pass#该片段来自于http://byrx.net
评论关闭