def _clear_files(self, slug, text): import re import itertools from uliweb.utils.image import fix_filename File = get_model("forumattachment") r_links = re.compile( r'<a.*?href=\"([^"\?]+)(?:\"|\?)|<img.*?src=\"([^"]+)\"|<embed.*?src=\"([^"]+)\"', re.DOTALL ) files = filter(None, itertools.chain(*re.findall(r_links, text))) for row in File.filter(File.c.slug == slug): _f = functions.get_filename(row.file_name) url = functions.get_href(row.file_name) if url in files: row.enabled = True row.save() else: if os.path.exists(_f): os.unlink(_f) thumbnail = fix_filename(_f, ".thumbnail") if os.path.exists(thumbnail): os.unlink(thumbnail) File.filter(File.c.slug == slug).filter(File.c.enabled == False).remove()
def _clear_files(self, slug, text): import re import itertools from uliweb.utils.image import fix_filename File = get_model('forumattachment') r_links = re.compile( r'<a.*?href=\"([^"\?]+)(?:\"|\?)|<img.*?src=\"([^"]+)\"|<embed.*?src=\"([^"]+)\"', re.DOTALL) files = filter(None, itertools.chain(*re.findall(r_links, text))) for row in File.filter(File.c.slug == slug): _f = functions.get_filename(row.file_name) url = functions.get_href(row.file_name) if url in files: row.enabled = True row.save() else: if os.path.exists(_f): os.unlink(_f) thumbnail = fix_filename(_f, '.thumbnail') if os.path.exists(thumbnail): os.unlink(thumbnail) File.filter(File.c.slug == slug).filter( File.c.enabled == False).remove()
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 deleteimage(self, id): """ 删除图片 :param id:图片id """ from uliweb.utils.image import fix_filename File = get_model('tutorials_albums') obj = File.get(int(id)) if obj: thumbnail_filename = fix_filename(obj.filename, '.thumbnail') functions.delete_filename(thumbnail_filename) functions.delete_filename(obj.filename) obj.delete() return json({'success':True, 'message':'删除成功'}) else: return json({'success':False, 'message':'图片没找到'})
def deleteimage(self, id): """ 删除图片 :param id:图片id """ from uliweb.utils.image import fix_filename File = get_model('tutorials_albums') obj = File.get(int(id)) if obj: thumbnail_filename = fix_filename(obj.filename, '.thumbnail') functions.delete_filename(thumbnail_filename) functions.delete_filename(obj.filename) obj.delete() return json({'success': True, 'message': '删除成功'}) else: return json({'success': False, 'message': '图片没找到'})
def getimages(self, tid): """ 返回图片 :param tid: tutorial id """ from uliweb.utils.image import thumbnail_image, fix_filename File = get_model('tutorials_albums') data = {'data':[], 'success':True} for row in File.filter(File.c.tutorial==int(tid)): d = {} thumbnail_filename = fix_filename(row.filename, '.thumbnail') d['thumbnail_url'] = functions.get_href(thumbnail_filename) d['url'] = functions.get_href(row.filename) d['id'] = row.id data['data'].append(d) return json(data)
def getimages(self, tid): """ 返回图片 :param tid: tutorial id """ from uliweb.utils.image import thumbnail_image, fix_filename File = get_model('tutorials_albums') data = {'data': [], 'success': True} for row in File.filter(File.c.tutorial == int(tid)): d = {} thumbnail_filename = fix_filename(row.filename, '.thumbnail') d['thumbnail_url'] = functions.get_href(thumbnail_filename) d['url'] = functions.get_href(row.filename) d['id'] = row.id data['data'].append(d) return json(data)