Ejemplo n.º 1
0
def _upload(request):
    """
  Handles file uploads. The uploaded file is stored in HDFS. We just
  need to rename it to the right path.
  """
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if not form.is_valid():
            logger.error("Error in upload form: %s" % (form.errors, ))
        else:
            uploaded_file = request.FILES['hdfs_file']
            dest = form.cleaned_data["dest"]
            if request.fs.isdir(dest):
                assert posixpath.sep not in uploaded_file.name
                dest = request.fs.join(dest, uploaded_file.name)

            # Temp file is created by superuser. Chown the file.
            tmp_file = uploaded_file.get_temp_path()
            username = request.user.username
            try:
                try:
                    request.fs.setuser(request.fs.superuser)
                    request.fs.chmod(tmp_file, 0644)
                    request.fs.chown(tmp_file, username, username)
                except IOError, ex:
                    msg = 'Failed to chown uploaded file ("%s") as superuser %s' % \
                          (tmp_file, request.fs.superuser)
                    logger.exception(msg)
                    raise PopupException(msg, detail=str(ex))
            finally:
                request.fs.setuser(username)

            # Move the file to where it belongs
            try:
                request.fs.rename(uploaded_file.get_temp_path(), dest)
            except IOError, ex:
                raise PopupException(
                    'Failed to rename uploaded temporary file ("%s") to "%s": %s'
                    % (tmp_file, dest, ex))

            dest_stats = request.fs.stats(dest)
            return render_with_toolbars(
                'upload_done.mako',
                request,
                {
                    # status is used by "fancy uploader"
                    'status': 1,
                    'path': dest,
                    'result': _massage_stats(request, dest_stats),
                    'next': request.GET.get("next")
                })
def file_process(request):
    query = request.GET
    path = get_path(query.get('dir', ''))

    abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)

    from filebrowser.forms import UploadForm
    form = UploadForm(data=request.POST, files=request.FILES, path=abs_path)

    if form.is_valid():
        f = form.cleaned_data['file']

        f.name = convert_filename(f.name)
        # PRE UPLOAD SIGNAL
        filebrowser_pre_upload.send(sender=request, path=abs_path, file=f)
        # HANDLE UPLOAD
        uploadedfile = handle_file_upload(abs_path, f)
        # POST UPLOAD SIGNAL
        filebrowser_post_upload.send(sender=request, path=abs_path, file=uploadedfile)
    else:
        return form.errors
Ejemplo n.º 3
0
            return render_with_toolbars(
                'upload_done.mako',
                request,
                {
                    # status is used by "fancy uploader"
                    'status': 1,
                    'path': dest,
                    'result': _massage_stats(request, dest_stats),
                    'next': request.GET.get("next")
                })
    else:
        dest = request.GET.get("dest")
        initial_values = {}
        if dest:
            initial_values["dest"] = dest
        form = UploadForm(initial=initial_values)
    return render_with_toolbars('upload.mako', request, {
        'form': form,
        'next': request.REQUEST.get("dest")
    })


def status(request):
    status = request.fs.status()
    data = {
        # Beware: "messages" is special in the context browser.
        'msgs': status.get_messages(),
        'health': status.get_health(),
        'datanode_report': status.get_datanode_report(),
        'name': request.fs.name
    }