def trash(self, system, file_path, trash_path): name = os.path.basename(file_path) f = BaseFileResource(self._ag, system, file_path) # first ensure trash_path exists BaseFileResource.ensure_path(self._ag, system, trash_path) # check if file with same name exists in trash; expect 404 try: check = os.path.join(trash_path, name) BaseFileResource.listing(self._ag, system, check) # if we did not 404, then we have a conflict; append date to name name_ext = os.path.splitext(name) timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') name = '{0} {1}{2}'.format(name_ext[0], timestamp, name_ext[1]) except HTTPError as e: if e.response.status_code != 404: raise resp = f.move(trash_path, name) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) parent_path = parent_path.strip('/') or '/' agave_indexer.apply_async(kwargs={ 'username': '******', 'systemId': system, 'filePath': os.path.dirname(f.path), 'recurse': False }, queue='indexing') return resp
def post(self, request, file_mgr_name, system_id, file_path): if file_mgr_name == AgaveFileManager.NAME \ or file_mgr_name == 'public': if not request.user.is_authenticated: return HttpResponseForbidden('Login required') agave_client = request.user.agave_oauth.client fm = AgaveFileManager(agave_client=agave_client) if request.FILES: upload_file = request.FILES['file'] upload_dir = file_path relative_path = request.POST.get('relative_path', None) if relative_path: # user uploaded a folder structure; ensure path exists upload_dir = os.path.join(file_path, os.path.dirname(relative_path)) BaseFileResource.ensure_path(agave_client, system_id, upload_dir) metrics.info('Data Depot', extra={ 'user': request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'data_depot_folder_upload', 'info': { 'filePath': file_path, 'relativePath': os.path.dirname(relative_path), 'systemId': system_id, 'uploadDir': upload_dir } }) try: result = fm.upload(system_id, upload_dir, upload_file) metrics.info('Data Depot', extra={ 'user': request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'data_depot_file_upload', 'info': { 'systemId': system_id, 'uploadDir': upload_dir, 'uploadFile': upload_file } }) result['system'] = result['systemId'] result_file = BaseFileResource(agave_client, **result) event_data = { Notification.EVENT_TYPE: 'data_depot', Notification.OPERATION: 'data_depot_file_upload', Notification.STATUS: Notification.SUCCESS, Notification.USER: request.user.username, Notification.MESSAGE: 'File Upload was successful.', Notification.EXTRA: result_file.to_dict() } Notification.objects.create(**event_data) except HTTPError as e: logger.error(e.response.text) event_data = { Notification.EVENT_TYPE: 'data_depot', Notification.OPERATION: 'data_depot_file_upload', Notification.STATUS: Notification.ERROR, Notification.USER: request.user.username, Notification.MESSAGE: 'There was an error uploading one or more file(s).', Notification.EXTRA: { 'system': system_id, 'file_path': file_path } } Notification.objects.create(**event_data) return HttpResponseBadRequest(e.response.text) return JsonResponse({'status': 'ok'}) return HttpResponseBadRequest("Unsupported operation")