Skip to content

Commit

Permalink
Saving external manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
brittnylapierre committed Nov 6, 2024
1 parent a7ebfb8 commit 482e901
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ async def create_files(prefix, noid, request: Request, authorized: bool = Depend
"message" : "You are not authorized to make this request."
}

manifest_noid = f'{prefix}/{noid}'
if manifest_noid == 'new/new':
manifest_noid = mint_noid("manifest")

data = await request.json() # This will give you a Python dictionary
# Step 2: Write the map to a JSON file
json_filename = f"{prefix}%2F{noid}.json"
Expand All @@ -220,8 +224,9 @@ async def create_files(prefix, noid, request: Request, authorized: bool = Depend
with open(json_filename, 'rb') as file:
timeout = httpx.Timeout(3000.0, read=3000.0)
with httpx.Client(timeout=timeout) as client:
expiration = datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(days=1)
token = jwt.encode(
{"pld": "editor-api-source"},
{"pld": "editor-api-source", "exp": expiration},
key=AAD_CLIENT_SECRET,
algorithm="HS256",
)
Expand All @@ -236,7 +241,7 @@ async def create_files(prefix, noid, request: Request, authorized: bool = Depend
response = client.put(url, files=file, headers=headers)
print(response.text)
if response.status_code == 200:
return {"success": True, "message": "Manifest saved"}
return {"success": True, "message": response.text}
else:
return {"success": False, "message": response.text}
else:
Expand Down Expand Up @@ -295,4 +300,53 @@ async def upload_files(prefix, noid, files: Annotated[List[bytes], File()], auth
"rendering": [{"id": f"{crkn_digirati_editor_api_url}/pdf/{canvas_noid}",
"type": "Text", "label": {"en": ["PDF version"]}, "format": "application/pdf"}],
})
return {"canvases": canvases}

@app.post("/createfilesfromurl/{prefix}/{noid}")
async def upload_files(prefix, noid, request: Request, authorized: bool = Depends(verify_token)):
if not authorized:
return {"message": "You are not authorized to make this request."}

manifest_noid = f'{prefix}/{noid}'
if manifest_noid == 'new/new':
manifest_noid = mint_noid("manifest")
print("body", await request.body())
data = await request.json() # This will give you a Python dictionary
canvases = []
for url in data['urls']:
canvas_noid = mint_noid("canvas")
encoded_canvas_noid = canvas_noid.replace("/", "%2F")
swift_filename = f"{canvas_noid}.jpg"
local_filename = f"{encoded_canvas_noid}.jpg"
response = requests.get(url)
if response.status_code != 200:
print("Failed to retrieve the image")
return {"message": "Could not get file"}
source_file = io.BytesIO((response.content))
convert_info = convert_image(source_file, local_filename)
swift_md5 = save_image_to_swift(local_filename, swift_filename, "access-files")
if swift_md5:
save_canvas(canvas_noid, encoded_canvas_noid, convert_info['width'], convert_info['height'], convert_info['size'], swift_md5)
canvases.append({
"id": f"{presentation_api_url}/canvas/{canvas_noid}",
"width": convert_info["width"],
"height": convert_info["height"],
"thumbnail": [{"id": f"{image_api_url}/iiif/2/{encoded_canvas_noid}/full/max/0/default.jpg", "type": "Image", "format": "image/jpeg"}],
"items": [{"id": f"{presentation_api_url}/{manifest_noid}/annotationpage/{canvas_noid}/main",
"type": "AnnotationPage", "items": [{"id": f"{presentation_api_url}/{manifest_noid}/annotation/{canvas_noid}/main/image",
"body": {"id": f"{image_api_url}/iiif/2/{encoded_canvas_noid}/full/max/0/default.jpg",
"type": "Image",
"width": convert_info["width"],
"height": convert_info["height"],
"format": "image/jpeg",
"service": [{"id": f"{image_api_url}/iiif/2/{encoded_canvas_noid}",
"type": "ImageService2", "profile": "level2"}]},
"type": "Annotation", "target": f"{presentation_api_url}/canvas/{canvas_noid}",
"motivation": "painting"}]}],
"seeAlso": [{"id": f"{crkn_digirati_editor_api_url}/ocr/{canvas_noid}", "type": "Dataset",
"label": {"en": ["Optical Character Recognition text in XML"]},
"format": "text/xml", "profile": "http://www.loc.gov/standards/alto"}],
"rendering": [{"id": f"{crkn_digirati_editor_api_url}/pdf/{canvas_noid}",
"type": "Text", "label": {"en": ["PDF version"]}, "format": "application/pdf"}],
})
return {"canvases": canvases}

0 comments on commit 482e901

Please sign in to comment.