- #上传图片文件
- 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://www.codesnippet.cn/detail/291020136733.html
来源: http://www.codesnippet.cn/detail/291020136733.html