diff --git a/applications/admin/controllers/default.py b/applications/admin/controllers/default.py index 6aefe163..c8949034 100644 --- a/applications/admin/controllers/default.py +++ b/applications/admin/controllers/default.py @@ -1185,7 +1185,74 @@ def plugin(): languages=languages, crontab=crontab) +def http_download_file(): + ''' + allows downloading files via http. + example: + in the input request.var.filename puts: + http://masonry.desandro.com/masonry.pkgd.min.js + This will download the file to the folder masonry.pkgd.min.js + automatically. + + for save it in a subdirectory alone passes the name of the folder: + js/http://masonry.desandro.com/masonry.pkgd.min.js + + ---- if does not exist the folder is created automatically. ---- + ''' + + import urllib2 + + if request.vars and not request.vars.token == session.token: + redirect(URL('logout')) + try: + filename = None + http = request.vars.location.split('/')[0] + app = get_app(name=http) + path = apath(request.vars.location, r=request) + + #nombre del archivo + if request.vars.filename: + filename = request.vars.filename.split('/')[-1] + else: + filename = re.sub('[^\w\./]+', '_', request.vars.filename) + + filename = os.path.join(path, filename) + dirpath = os.path.dirname(filename) + + #recuperamos el registro http + newdir = dirpath + '/' + request.vars.filename.split('/')[0] + insert = request.vars.filename.split('http://') + insert.append('http://') + + if not os.path.exists(newdir): + os.makedirs(newdir) + + urls = insert[-1] + insert[-2] + get_url = urllib2.urlopen(urls) + + data = get_url.read() + fil = request.vars.filename.split('http://') + lineno = count_lines(data) + if fil[0] != '': + filename = filename.split('/') + filename.insert(-1, fil[0][:-1]) + ruta = '/' + '/'.join(filename) + safe_write(ruta, data, 'wb') + else: + safe_write(filename, data, 'wb') + log_progress(app, 'HTTP', filename, lineno) + session.flash = T('file "%(filename)s" downloaded', + dict(filename=request.vars.filename.split('/')[-1])) + except Exception: + if filename: + d = dict(filename=filename[len(path):]) + else: + d = dict(filename='unkown') + session.flash = T('cannot download file "%(filename)s"', d) + + redirect(request.vars.sender) + def create_file(): """ Create files handler """ if request.vars and not request.vars.token == session.token: diff --git a/applications/admin/views/default/design.html b/applications/admin/views/default/design.html index c9ed67a2..86077980 100644 --- a/applications/admin/views/default/design.html +++ b/applications/admin/views/default/design.html @@ -33,6 +33,19 @@ def file_upload_form(location, anchor=None): _action=URL('upload_file'), _class="generatedbyw2p well well-small") return form + +def http_download_file(location, anchor=None): + form=FORM( + LABEL(T("download files via http:")), + INPUT(_type="text",_name="filename",requires=IS_NOT_EMPTY,_class=""), + DIV(TAG['BUTTON'](T("Download"),_type="submit",_class="btn"),_class="controls"), + INPUT(_type="hidden",_name="location",_value=location), + INPUT(_type="hidden",_name="token",_value=session.token), + INPUT(_type="hidden",_name="sender",_value=URL('design',args=app, anchor=anchor)), + _action=URL('http_download_file'), + _class="generatedbyw2p well well-small") + return form + def file_create_form(location, anchor=None, helptext=""): form=FORM( LABEL(T("create file with filename:")), @@ -336,6 +349,7 @@ for c in controllers: controller_functions+=[c[:-3]+'/%s.html'%x for x in functi