def _upload_file(request): """ Handles file uploaded by HDFSfileUploadHandler. The uploaded file is stored in HDFS. We just need to rename it to the destination path. """ form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['hdfs_file'] dest = form.cleaned_data['dest'] if request.fs.isdir(dest) and posixpath.sep in uploaded_file.name: raise PopupException( _('Sorry, no "%(sep)s" in the filename %(name)s.' % { 'sep': posixpath.sep, 'name': uploaded_file.name })) dest = request.fs.join(dest, uploaded_file.name) tmp_file = uploaded_file.get_temp_path() username = request.user.username try: # Temp file is created by superuser. Chown the file. request.fs.do_as_superuser(request.fs.chmod, tmp_file, 0644) request.fs.do_as_superuser(request.fs.chown, tmp_file, username, username) # Move the file to where it belongs request.fs.rename(tmp_file, dest) 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) return { 'status': 0, 'path': dest, 'result': _massage_stats(request, request.fs.stats(dest)), 'next': request.GET.get("next") }
def _upload_file(request): """ Handles file uploaded by HDFSfileUploadHandler. The uploaded file is stored in HDFS at its destination with a .tmp suffix. We just need to rename it to the destination path. """ form = UploadFileForm(request.POST, request.FILES) response = {'status': -1, 'data': ''} if request.META.get('upload_failed'): raise PopupException(request.META.get('upload_failed')) if form.is_valid(): uploaded_file = request.FILES['hdfs_file'] dest = form.cleaned_data['dest'] if request.fs.isdir(dest) and posixpath.sep in uploaded_file.name: raise PopupException(_('Sorry, no "%(sep)s" in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name})) dest = request.fs.join(dest, uploaded_file.name) tmp_file = uploaded_file.get_temp_path() username = request.user.username try: # Remove tmp suffix of the file request.fs.do_as_user(username, request.fs.rename, tmp_file, dest) 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
def _upload_file(request): """ Handles file uploaded by HDFSfileUploadHandler. The uploaded file is stored in HDFS. We just need to rename it to the destination path. """ form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['hdfs_file'] dest = form.cleaned_data['dest'] if request.fs.isdir(dest) and posixpath.sep in uploaded_file.name: raise PopupException(_('Sorry, no "%(sep)s" in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name})) dest = request.fs.join(dest, uploaded_file.name) tmp_file = uploaded_file.get_temp_path() username = request.user.username try: # Temp file is created by superuser. Chown the file. request.fs.do_as_superuser(request.fs.chmod, tmp_file, 0644) request.fs.do_as_superuser(request.fs.chown, tmp_file, username, username) # Move the file to where it belongs request.fs.rename(tmp_file, dest) 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) return { 'status': 0, 'path': dest, 'result': _massage_stats(request, request.fs.stats(dest)), 'next': request.GET.get("next") }