Ejemplo n.º 1
0
Archivo: views.py Proyecto: hqren/hue
def _upload_archive(request):
    """
    Handles archive upload.
    The uploaded file is stored in memory.
    We need to extract it and rename it.
    """
    form = UploadArchiveForm(request.POST, request.FILES)
    response = {'status': -1, 'data': ''}

    if form.is_valid():
        uploaded_file = request.FILES['archive']

        # Always a dir
        if request.fs.isdir(form.cleaned_data['dest']) and posixpath.sep in uploaded_file.name:
            raise PopupException(_('No "%(sep)s" allowed in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name}))

        dest = request.fs.join(form.cleaned_data['dest'], uploaded_file.name)
        try:
            # Extract if necessary
            # Make sure dest path is without the extension
            if dest.endswith('.zip'):
                temp_path = archive_factory(uploaded_file, 'zip').extract()
                if not temp_path:
                    raise PopupException(_('Could not extract contents of file.'))
                # Move the file to where it belongs
                dest = dest[:-4]
            elif dest.endswith('.tar.gz'):
                print uploaded_file
                temp_path = archive_factory(uploaded_file, 'tgz').extract()
                if not temp_path:
                    raise PopupException(_('Could not extract contents of file.'))
                # Move the file to where it belongs
                dest = dest[:-7]
            else:
                raise PopupException(_('Could not interpret archive type.'))

            request.fs.copyFromLocal(temp_path, dest)
            shutil.rmtree(temp_path)
            response['status'] = 0

        except IOError, ex:
            already_exists = False
            try:
                already_exists = request.fs.exists(dest)
            except Exception:
              pass
            if already_exists:
                msg = _('Destination %(name)s already exists.') % {'name': dest}
            else:
                msg = _('Copy to %(name)s failed: %(error)s') % {'name': dest, 'error': ex}
            raise PopupException(msg)

        response.update({
          'path': dest,
          'result': _massage_stats(request, request.fs.stats(dest)),
          'next': request.GET.get("next")
        })

        return response
Ejemplo n.º 2
0
def _upload_archive(request):
    """
    Handles archive upload.
    The uploaded file is stored in memory.
    We need to extract it and rename it.
    """
    form = UploadArchiveForm(request.POST, request.FILES)
    response = {'status': -1, 'data': ''}

    if form.is_valid():
        uploaded_file = request.FILES['archive']

        # Always a dir
        if request.fs.isdir(form.cleaned_data['dest']) and posixpath.sep in uploaded_file.name:
            raise PopupException(_('No "%(sep)s" allowed in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name}))

        dest = request.fs.join(form.cleaned_data['dest'], uploaded_file.name)
        try:
            # Extract if necessary
            # Make sure dest path is without the extension
            if dest.endswith('.zip'):
                temp_path = archive_factory(uploaded_file, 'zip').extract()
                if not temp_path:
                    raise PopupException(_('Could not extract contents of file.'))
                # Move the file to where it belongs
                dest = dest[:-4]
            elif dest.endswith('.tar.gz') or dest.endswith('.tgz'):
                print uploaded_file
                temp_path = archive_factory(uploaded_file, 'tgz').extract()
                if not temp_path:
                    raise PopupException(_('Could not extract contents of file.'))
                # Move the file to where it belongs
                dest = dest[:-7]
            else:
                raise PopupException(_('Could not interpret archive type.'))

            request.fs.copyFromLocal(temp_path, dest)
            shutil.rmtree(temp_path)
            response['status'] = 0

        except IOError, ex:
            already_exists = False
            try:
                already_exists = request.fs.exists(dest)
            except Exception:
              pass
            if already_exists:
                msg = _('Destination %(name)s already exists.') % {'name': dest}
            else:
                msg = _('Copy to %(name)s failed: %(error)s') % {'name': dest, 'error': ex}
            raise PopupException(msg)

        response.update({
          'path': dest,
          'result': _massage_stats(request, request.fs.stats(dest)),
          'next': request.GET.get("next")
        })

        return response