def upload_image(request): upload_data = json.loads(request.body.decode()) image_type = upload_data['image_type'] image_data = base64.b64decode(upload_data['image_data']) prefix = upload_data.get('prefix') or '' if settings.PRODUCTION: prefix = os.path.join('production', prefix) else: prefix = os.path.join('development', prefix) return {'url': upload_editor_image(image_data, image_type, prefix)}
def upload_image(request): upload_data = json.loads(request.body.decode()) image_type = upload_data["image_type"] image_data = base64.b64decode(upload_data["image_data"]) prefix = upload_data.get("prefix") or "" if settings.PRODUCTION: prefix = os.path.join("production", prefix) else: prefix = os.path.join("development", prefix) return {"url": upload_editor_image(image_data, image_type, prefix)}
def upload_image(request): upload_data = load_encoded_json(request.body) image_type = upload_data['image_type'] image_data = base64.b64decode(upload_data['image_data']) prefix = upload_data.get('prefix') or '' if settings.PRODUCTION: prefix = os.path.join('production', prefix) else: prefix = os.path.join('development', prefix) return {'url': upload_editor_image(image_data, image_type, prefix)}
def test_upload_editor_image(self, boto_patch): # Sample unicode data string sample_data = 'data'.encode() # Attempt to upload incorrect image type with self.assertRaises(S3UploadError): upload_editor_image(sample_data, 'invalid_type') # Upload supported image type upload_editor_image(sample_data, 'image/jpeg') max_size_bytes = MAX_UPLOAD_SIZE_MB * 10 ** 6 # Attempt to upload image larger than size limit sample_data = ('a' * math.floor(max_size_bytes * 1.0001)).encode() with self.assertRaises(S3UploadError): upload_editor_image(sample_data, 'image/jpeg') # Upload image smaller than size limit sample_data = ('a' * math.floor(max_size_bytes * 0.9999)).encode() upload_editor_image(sample_data, 'image/jpeg')