def post(self, request): """ Create a new asset """ tags = request.DATA.get('tags', '') optimize = request.DATA.get('optimize', False) asset = request.DATA.get('asset') friendly_name = request.DATA.get('friendly-name') url_path = request.DATA.get('url-path', '').strip('/') try: url_path = create_asset(file_data=b64decode(asset), friendly_name=friendly_name, tags=tags, url_path=url_path, optimize=optimize) except IOError as create_error: if create_error.errno == errno.EEXIST: return error_response(create_error) else: raise create_error except SwiftClientException as swift_error: return error_response(swift_error, url_path) # Return the list of data for the created files return Response(settings.DATA_MANAGER.fetch_one(url_path), 201)
def get(self, request, file_path): """ Get a single asset, 404ing if we get an OSError. """ try: asset_data = settings.FILE_MANAGER.fetch(file_path) asset_headers = settings.FILE_MANAGER.headers(file_path) except SwiftClientException as error: return error_response(error, file_path) time_format = '%a, %d %b %Y %H:%M:%S %Z' def make_datetime(x): return datetime.strptime(x, time_format) last_modified = asset_headers['last-modified'] if_modified_since = request.META.get( 'HTTP_IF_MODIFIED_SINCE', 'Mon, 1 Jan 1980 00:00:00 GMT' ) if make_datetime(last_modified) <= make_datetime(if_modified_since): return HttpResponseNotModified() # Run image processor try: image = ImageProcessor( asset_data, request.GET ) converted_type = image.process() asset_data = image.data except (PilboxError, ValueError) as error: return error_response(error, file_path) # Get a sensible filename, including a converted extension filename = remove_filename_hash(file_path) if converted_type: filename = '{0}.{1}'.format(filename, converted_type) # Start response, guessing mime type response = HttpResponse( asset_data, content_type=get_mimetype(filename) ) # Set download filename response['Content-Disposition'] = "filename={}".format(filename) # Cache all genuine assets forever response['Cache-Control'] = 'max-age=31556926' response['Last-Modified'] = last_modified # Return asset return set_headers_for_type(response)
def delete(self, request, file_path): """ Delete a single named asset, 204 if successful """ try: settings.DATA_MANAGER.delete(file_path) settings.FILE_MANAGER.delete(file_path) return Response({"message": "Deleted {0}".format(file_path)}) except SwiftClientException as err: return error_response(err, file_path)
def get(self, request, file_path): """ Get data for an asset by path """ if settings.DATA_MANAGER.exists(file_path): response = Response(settings.DATA_MANAGER.fetch_one(file_path), headers={'Cache-Control': 'no-cache'}) else: asset_error = file_error( error_number=errno.ENOENT, message="No JSON data found for file {0}".format(file_path), filename=file_path) response = error_response(asset_error) return response