def uploadimage(self, tid): """ 上传图片 :param tid: 论坛id """ import Image from uliweb.utils.image import thumbnail_image, fix_filename from uliweb import json_dumps from uliweb.form import Form, ImageField File = get_model('tutorials_albums') obj = self.model.get_or_notfound(int(tid)) class UploadForm(Form): filename = ImageField() form = UploadForm() flag = form.validate(request.values, request.files) if flag: filename = functions.save_file(os.path.join('tutorials', str(obj.id), form.filename.data.filename), form.filename.data.file) #process thumbnail rfilename, thumbnail = thumbnail_image(functions.get_filename(filename, filesystem=True), filename, settings.get_var('TUTORIALS/IMAGE_THUMBNAIL_SIZE')) thumbnail_url = functions.get_href(thumbnail) url = functions.get_href(filename) f = File(filename=filename, tutorial=int(tid)) f.save() return json({'success':True, 'filename':form.filename.data.filename, 'url':url, 'thumbnail_url':thumbnail_url, 'id':f.id}, content_type="text/html;charset=utf-8") else: #如果校验失败,则再次返回Form,将带有错误信息 return json({'success':False}, content_type="text/html;charset=utf-8")
def postimage(self, tid): """ 处理HTML5文件上传 :param tid:教程id """ import base64 from StringIO import StringIO from uliweb.utils.image import thumbnail_image, fix_filename File = get_model('tutorials_albums') if request.method == 'POST': _filename = os.path.join('tutorial/%s' % tid, request.values.get('filename')) fobj = StringIO(base64.b64decode(request.params.get('data'))) filename = functions.save_file(_filename, fobj) #process thumbnail rfilename, thumbnail = thumbnail_image( functions.get_filename(filename, filesystem=True), filename, settings.get_var('TUTORIALS/IMAGE_THUMBNAIL_SIZE')) thumbnail_url = functions.get_href(thumbnail) url = functions.get_href(filename) f = File(filename=filename, tutorial=int(tid)) f.save() return json({ 'success': True, 'data': { 'filename': request.values.get('filename'), 'url': url, 'thumbnail_url': thumbnail_url, 'id': f.id } })
def _upload_file(self, image=False, show_filename=True): import os import Image if image: from forms import ImageUploadForm as Form else: from forms import FileUploadForm as Form from uliweb.utils.image import thumbnail_image, fix_filename from uliweb import json_dumps File = get_model('forumattachment') forum_id = request.GET.get('forum_id') slug = request.GET.get('slug') form = Form() suffix = date.now().strftime('_%Y_%m_%d') if request.method == 'GET': form.bind({'is_thumbnail': True}) return {'form': form} else: flag = form.validate(request.values, request.files) if flag: f = form.data['filedata'] _f = os.path.basename(f['filename']) #文件格式为forum/<forum_id>/<filename_yyyy_mm_dd> filename = fix_filename('forum/%s/%s' % (forum_id, _f), suffix) if image: filename = functions.save_file(filename, f['file']) if form.data['is_thumbnail']: #process thumbnail rfilename, thumbnail = thumbnail_image( functions.get_filename(filename, filesystem=True), filename, settings.get_var('PARA/FORUM_THUMBNAIL_SIZE')) _file = functions.get_href(thumbnail) else: _file = functions.get_href(filename) name = functions.get_href(filename) else: filename = functions.save_file(filename, f['file']) name = form.data['title'] if not name: name = _f _file = functions.get_href(filename, alt=name) ff = File(slug=slug, file_name=filename, name=name) ff.save() name = json_dumps(name, unicode=True) if show_filename: fargs = '||%s' % name[1:-1] else: fargs = '' return '''<script type="text/javascript"> var url='%s%s'; setTimeout(function(){callback(url);},100); </script> ''' % (_file, fargs) else: return {'form': form}
def _upload_file(self, image=False): import os import Image from uliweb.contrib.upload import get_url, save_file, get_filename if image: from forms import ImageUploadForm as Form else: from forms import FileUploadForm as Form from uliweb.utils.image import thumbnail_image, fix_filename File = get_model("forumattachment") forum_id = request.GET.get("forum_id") slug = request.GET.get("slug") form = Form() suffix = date.now().strftime("_%Y_%m_%d") if request.method == "GET": form.bind({"is_thumbnail": True}) return {"form": form} else: flag = form.validate(request.values, request.files) if flag: f = form.data["filedata"] _f = os.path.basename(f["filename"]) # 文件格式为forum/<forum_id>/<filename_yyyy_mm_dd> filename = fix_filename("forum/%s/%s" % (forum_id, _f), suffix) if image: filename = save_file(filename, f["file"]) if form.data["is_thumbnail"]: # process thumbnail rfilename, thumbnail = thumbnail_image( get_filename(filename, filesystem=True), filename, settings.get_var("PARA/FORUM_THUMBNAIL_SIZE"), ) _file = get_url(thumbnail) else: _file = get_url(filename) name = get_url(filename) else: filename = save_file(filename, f["file"]) _file = get_url(filename) name = form.data["title"] if not name: name = _f ff = File(slug=slug, file_name=filename, name=name) ff.save() return """<script type="text/javascript"> var url='%s||%s'; setTimeout(function(){callback(url);},100); </script> """ % ( _file, name, ) else: return {"form": form}
def _upload_file(self, image=False, show_filename=True): import os import Image if image: from forms import ImageUploadForm as Form else: from forms import FileUploadForm as Form from uliweb.utils.image import thumbnail_image, fix_filename from uliweb import json_dumps File = get_model('forumattachment') forum_id = request.GET.get('forum_id') slug = request.GET.get('slug') form = Form() suffix = date.now().strftime('_%Y_%m_%d') if request.method == 'GET': form.bind({'is_thumbnail':True}) return {'form':form} else: flag = form.validate(request.values, request.files) if flag: f = form.data['filedata'] _f = os.path.basename(f['filename']) #文件格式为<forum_id>/<filename_yyyy_mm_dd> filename = fix_filename('%s/%s' % (forum_id, _f), suffix) if image: filename = functions.save_file(filename, f['file']) if form.data['is_thumbnail']: #process thumbnail rfilename, thumbnail = thumbnail_image(functions.get_filename(filename, filesystem=True), filename, settings.get_var('PARA/FORUM_THUMBNAIL_SIZE')) _file = functions.get_href(thumbnail) else: _file = functions.get_href(filename) name = functions.get_href(filename) else: filename = functions.save_file(filename, f['file']) name = form.data['title'] if not name: name = _f _file = functions.get_href(filename, alt=name) ff = File(slug=slug, file_name=filename, name=name) ff.save() name = json_dumps(name, unicode=True) if show_filename: fargs = '||%s' % name[1:-1] else: fargs = '' return '''<script type="text/javascript"> var url='%s%s'; setTimeout(function(){callback(url);},100); </script> ''' % (_file, fargs) else: return {'form':form}
def uploadfile(): from uliweb.form import Form, FileField from uliweb.utils.image import test_image, thumbnail_image fileserving = AttachmentsFileServing() Attachment = functions.get_model('generic_attachment') class UploadForm(Form): fileupload = FileField() def form_validate(self, data): error = {} return error form = UploadForm() table = request.GET.get('table') id = request.GET.get('id') slug = request.GET.get('slug') if form.validate(request.values, request.files): dir = settings.Generic_Attachment_Save_Dirs.get(table) if not dir: raise Exception("Saving directory of table %s is not defined in Generic_Attachment_Save_Dirs" % table) if id: path = os.path.join(dir, id) else: path = dir filename = fileserving.save_file(os.path.join(path, form.fileupload.data.filename), form.fileupload.data.file) is_image = test_image(filename) if is_image and settings.get_var('Generic_Attachment_FileServing/THUMBNAIL_PROCESS'): thumbnail_path, thumbnail = thumbnail_image(functions.get_filename(filename, filesystem=True), filename, settings.get_var('Generic_Attachment_FileServing/IMAGE_THUMBNAIL_SIZE')) thumbnail_url = functions.get_href(thumbnail) else: thumbnail = '' thumbnail_url = '' f = Attachment(filepath=filename, filename=form.fileupload.data.filename, submitter=request.user.id, is_image=is_image, thumbnail_path=thumbnail) if slug: f.slug = slug else: f.content_object = (table, id) f.save() url = fileserving.get_url(filename, title=f.filename, query_para={'alt':f.filename}, _class='filedown') href = fileserving.get_href(filename, alt=f.filename) return json({'success':True, 'filename':f.filename, 'url':url, 'href':href, 'id':f.id, 'submitter':unicode(request.user), 'is_image':is_image, 'thumbnail_url':thumbnail_url, 'created_date':str(f.created_date)}, content_type="text/html;charset=utf-8") else: #如果校验失败,则再次返回Form,将带有错误信息 return json({'success':False}, content_type="text/html;charset=utf-8")
def postimage(): """ 处理HTML5文件上传 """ import base64 from StringIO import StringIO from uliweb.utils.image import thumbnail_image, fix_filename from uliweb.form import Form, FileField fileserving = AttachmentsFileServing() Attachment = functions.get_model('generic_attachment') table = request.values.get('table') id = request.values.get('id') slug = request.values.get('slug') if request.method == 'POST': dir = settings.Generic_Attachment_Save_Dirs.get(table) if not dir: raise Exception("Saving directory of table %s is not defined in Generic_Attachment_Save_Dirs" % table) if id: path = os.path.join(dir, id, request.values.get('filename')) else: path = os.path.join(dir, request.values.get('filename')) fobj = StringIO(base64.b64decode(request.params.get('data'))) filename = functions.save_file(path, fobj) if settings.get_var('Generic_Attachment_FileServing/THUMBNAIL_PROCESS'): thumbnail_path, thumbnail = thumbnail_image(functions.get_filename(filename, filesystem=True), filename, settings.get_var('Generic_Attachment_FileServing/IMAGE_THUMBNAIL_SIZE')) thumbnail_url = functions.get_href(thumbnail) else: thumbnail = '' thumbnail_url = '' fname = os.path.splitext(os.path.basename(filename))[0].split(',')[-1][:8] f = Attachment(filepath=filename, filename=fname, is_image=True, submitter=request.user.id, thumbnail_path=thumbnail) if slug: f.slug = slug else: f.content_object = (table, id) f.save() url = fileserving.get_url(filename, title=f.filename, alt=f.filename, _class='filedown') href = fileserving.get_href(filename, title=f.filename, query_para={'alt':f.filename}) return json({'success':True, 'filename':f.filename, 'submitter':unicode(request.user), 'url':url, 'href':href, 'is_image':f.is_image, 'id':f.id, 'created_date':str(f.created_date), 'thumbnail_url':thumbnail_url, })
def uploadimage(self, tid): """ 上传图片 :param tid: 论坛id """ import Image from uliweb.utils.image import thumbnail_image, fix_filename from uliweb import json_dumps from uliweb.form import Form, ImageField File = get_model('tutorials_albums') obj = self.model.get_or_notfound(int(tid)) class UploadForm(Form): filename = ImageField() form = UploadForm() flag = form.validate(request.values, request.files) if flag: filename = functions.save_file( os.path.join('tutorials', str(obj.id), form.filename.data.filename), form.filename.data.file) #process thumbnail rfilename, thumbnail = thumbnail_image( functions.get_filename(filename, filesystem=True), filename, settings.get_var('TUTORIALS/IMAGE_THUMBNAIL_SIZE')) thumbnail_url = functions.get_href(thumbnail) url = functions.get_href(filename) f = File(filename=filename, tutorial=int(tid)) f.save() return json( { 'success': True, 'filename': form.filename.data.filename, 'url': url, 'thumbnail_url': thumbnail_url, 'id': f.id }, content_type="text/html;charset=utf-8") else: #如果校验失败,则再次返回Form,将带有错误信息 return json({'success': False}, content_type="text/html;charset=utf-8")
def postimage(self, tid): """ 处理HTML5文件上传 :param tid:教程id """ import base64 from StringIO import StringIO from uliweb.utils.image import thumbnail_image, fix_filename File = get_model('tutorials_albums') if request.method == 'POST': _filename = os.path.join('tutorial/%s' % tid, request.values.get('filename')) fobj = StringIO(base64.b64decode(request.params.get('data'))) filename = functions.save_file(_filename, fobj) #process thumbnail rfilename, thumbnail = thumbnail_image(functions.get_filename(filename, filesystem=True), filename, settings.get_var('TUTORIALS/IMAGE_THUMBNAIL_SIZE')) thumbnail_url = functions.get_href(thumbnail) url = functions.get_href(filename) f = File(filename=filename, tutorial=int(tid)) f.save() return json({'success':True, 'data':{'filename':request.values.get('filename'), 'url':url, 'thumbnail_url':thumbnail_url, 'id':f.id}})