Example #1
0
File: main.py Project: d5/daniel.gs
    def _render(file_path):
        if not file_path.startswith('/'):
            # make it a full-path
            if g.file.is_dir:        
                file_path = os.path.join(g.file.path, file_path)
            else:
                file_path = os.path.join(os.path.dirname(g.file.path), file_path)

        df = g.open(file_path)
        if not df:
            return 'File not found: '+file_path if g.debug else ''    

        return df.rendered_content
Example #2
0
File: main.py Project: d5/daniel.gs
def update(path):
    if not is_current_user_author():
        return abort(401)

    prev_url = request.form.get('prev_url', '/')

    g.file = g.open(path)
    content = request.form['value']
    if g.file:
        g.file.write(content)
        flash('The file was successfully updated: '+path)
    else:
        g.author.dropbox_client.put_file(path, content, overwrite=True)

    return redirect(prev_url)
Example #3
0
File: dbx.py Project: d5/daniel.gs
 def parent(self):
     return g.open(os.path.dirname(self.path))
Example #4
0
File: main.py Project: d5/daniel.gs
def view(path=''):
    if app.debug and request.args.get('md', None) == '1':
        md = g.author.dropbox_client.metadata(path, list=True, include_deleted=False)
        return Response(str(md), mimetype='application/json')

    g.file = g.open(path)

    # file staring with '!' is author-access only
    if g.file and not is_current_user_author():
        for t in g.file.path.split('/'):
            if t.startswith('!'):
                return abort(401)


    if g.file is None and not 'edit' in request.args:
        # File Not Found error
        return abort(404)    

    # handle directory path
    if g.file and g.file.is_dir:
        df = g.file.get_file(g.file.dirinfo.default_file)
        if df and not 'browse' in request.args:
            return view(df.path)
        else:
            return render('browse.html')

    # deleting a file
    if g.file and 'delete' in request.args:
        # only author account can delete files
        if not is_current_user_author():
            return abort(401)        

        prev_url = request.args.get('purl', '/')

        g.author.dropbox_client.file_delete(g.file.path)

        return redirect(prev_url)


    # editing file content
    if not g.file or (g.file.is_editable and 'edit' in request.args):
        # only author account can edit the contents
        if not is_current_user_author():
            return abort(401)

        prev_url = request.args.get('purl', '/')
        title = g.file.file_name if g.file else os.path.basename(request.path)
        title += ' - edit' if g.file else ' - create'
        mime_type = request.args.get('mime', None)
        if not mime_type:
            mime_type = g.file.mime_type if g.file else get_mime_type(request.path)
        content = g.file.content if g.file else ''

        return render('edit.html', prev_url=prev_url, title=title, mime_type=mime_type, content=content)

    # image files
    if g.file.is_image:
        tn_size = request.args.get('tn', None)
        if tn_size:
            # stream thumbnail data
            return Response(g.file.thumbnail(size=tn_size), mimetype='image/jpeg')
        else:
            # redirect to Dropbox direct link
            return redirect(g.file.direct_link)

    # CSS/JavaScript files
    if g.file.mime_type in ['text/css', 'application/x-javascript', 'text/javascript']:
        # redirect to Dropbox direct link
        return redirect(g.file.direct_link)    

    # unknown type files
    if g.file.is_renderable:
        # if the content is renderable: render the contents
        return Response(g.file.rendered_content, mimetype='text/html')
    
    # by default, just stream the un-altered content
    return Response(g.file.content, mimetype=g.file.mime_type)