|
26 | 26 | from typing import Final, List |
27 | 27 |
|
28 | 28 | import arweave |
29 | | -import bagit |
30 | 29 | import humanize |
31 | 30 | import requests |
32 | 31 | from arweave.arweave_lib import Transaction, arql |
@@ -322,55 +321,10 @@ async def _fetch_upload(transaction_id: str) -> FileResponse: |
322 | 321 | raise HTTPException from err |
323 | 322 |
|
324 | 323 |
|
325 | | -async def bag_files(path: Path, tag_list=None) -> None: |
326 | | - """Use python Bagit to bag the files for Arkly-Arweave.""" |
327 | | - if not tag_list: |
328 | | - bagit.make_bag(path, {PACKAGING_AGENT_STRING: ARKLY_AGENT}) |
329 | | - return |
330 | | - bag_info = {} |
331 | | - for tag in tag_list: |
332 | | - bag_info[f"{tag.name}".replace(" ", "-")] = tag.value |
333 | | - bag_info[PACKAGING_AGENT_STRING] = ARKLY_AGENT |
334 | | - logger.info("writing package with bag-info: %d", bag_info) |
335 | | - bagit.make_bag(path, bag_info) |
336 | | - return |
337 | | - |
338 | | - |
339 | | -async def _package_content( |
340 | | - files: List[UploadFile] = File(...), package_name: str = None, tag_list: list = None |
341 | | -) -> dict: |
342 | | - """Package the files submitted to the create_transaction endpoint.""" |
343 | | - # Create a folder for the user's wallet. |
344 | | - tmp_dir = tempfile.mkdtemp() |
345 | | - file_path = Path(tmp_dir, package_name) |
346 | | - file_path.mkdir() |
347 | | - |
348 | | - logger.info("Location to write object to: %s", file_path) |
349 | | - |
350 | | - for file in files: |
351 | | - read_file = await file.read() |
352 | | - output_file = Path(file_path, file.filename) |
353 | | - output_file.write_bytes(read_file) |
354 | | - |
355 | | - # Bag these files. |
356 | | - await bag_files(file_path, tag_list) |
357 | | - |
358 | | - # Create compressed .tar.gz file |
359 | | - tar_file_name = file_path.with_suffix(".tar.gz") |
360 | | - with tarfile.open(tar_file_name, "w:gz") as tar: |
361 | | - tar.add(file_path, arcname=os.path.basename(file_path)) |
362 | | - |
363 | | - version_api: Final[str] = "v0" |
364 | | - tar_file_name = tar_file_name.rename( |
365 | | - f"{tar_file_name}".replace(".tar.gz", f"_{version_api}.tar.gz") |
366 | | - ) |
367 | | - return tar_file_name |
368 | | - |
369 | | - |
370 | 324 | async def _create_transaction( |
371 | 325 | wallet: UploadFile, |
372 | | - files: List[UploadFile] = File(...), |
373 | | - package_file_name: str = None, |
| 326 | + files: list[UploadFile] = File(...), |
| 327 | + mime_type: str = None, |
374 | 328 | tags: Tags = None, |
375 | 329 | ) -> dict: |
376 | 330 | """Create an Arkly package and Arweave transaction. |
@@ -398,21 +352,29 @@ async def _create_transaction( |
398 | 352 | except AttributeError: |
399 | 353 | logger.info("no user-defined tags provided by caller") |
400 | 354 |
|
401 | | - # Create a package from files array. Package content will create |
402 | | - # this in a secure temporary directory. |
403 | | - tar_file_name = await _package_content(files, package_file_name, tag_list) |
404 | | - |
405 | | - logger.info("Adding version to package: %s", tar_file_name) |
406 | | - logger.info("New path exists: %s", tar_file_name.is_file()) |
407 | 355 | logger.info("Wallet balance before upload: %s", wallet.balance) |
408 | 356 |
|
409 | | - with open(tar_file_name, "rb", buffering=0) as file_handler: |
| 357 | + tmp_dir = tempfile.mkdtemp() |
| 358 | + file_path = Path(tmp_dir, "upload_path") |
| 359 | + file_path.mkdir() |
| 360 | + |
| 361 | + logger.info("Location to write object to: %s", file_path) |
| 362 | + |
| 363 | + output_file = None |
| 364 | + for file in files: |
| 365 | + read_file = await file.read() |
| 366 | + output_file = Path(file_path, file.filename) |
| 367 | + output_file.write_bytes(read_file) |
| 368 | + break |
| 369 | + |
| 370 | + logger.info("attempting to upload: %s", output_file) |
| 371 | + |
| 372 | + with open(output_file, "rb", buffering=0) as file_handler: |
410 | 373 | new_transaction = Transaction( |
411 | | - wallet, file_handler=file_handler, file_path=tar_file_name |
| 374 | + wallet, file_handler=file_handler, file_path=output_file, |
412 | 375 | ) |
413 | 376 | # Default tags for the tar/gzip file that we create. |
414 | | - new_transaction.add_tag("Content-Type", "application/gzip") |
415 | | - |
| 377 | + new_transaction.add_tag("Content-Type", mime_type) |
416 | 378 | for tag in tag_list: |
417 | 379 | logger.info("Adding tag: %s: %s", tag.name, tag.value) |
418 | 380 | new_transaction.add_tag(tag.name, tag.value) |
@@ -445,60 +407,6 @@ def _get_arweave_urls_from_tx(transaction_id: str) -> dict: |
445 | 407 | ) |
446 | 408 |
|
447 | 409 |
|
448 | | -async def _validate_bag(transaction_id: str, response: Response) -> dict: |
449 | | - """Given an Arweave transaction ID, Validate an Arkly link as a bag.""" |
450 | | - |
451 | | - # Setup retrieval of the data from the given transaction. |
452 | | - transaction_url, arweave_url = _get_arweave_urls_from_tx(transaction_id) |
453 | | - arweave_response = requests.get(arweave_url, allow_redirects=True) |
454 | | - |
455 | | - # Create temp file to extract the contents from Arweave to. |
456 | | - tmp_file_handle, tmp_file_path = tempfile.mkstemp() |
457 | | - with open(tmp_file_handle, "wb") as write_tar_gz: |
458 | | - write_tar_gz.write(arweave_response.content) |
459 | | - |
460 | | - tmp_dir = tempfile.mkdtemp() |
461 | | - try: |
462 | | - arkly_gzip = tarfile.open(tmp_file_path) |
463 | | - arkly_gzip.extractall(tmp_dir) |
464 | | - except tarfile.ReadError: |
465 | | - response.status_code = status.HTTP_422_UNPROCESSABLE_ENTITY |
466 | | - return { |
467 | | - "transaction_url": transaction_url, |
468 | | - "file_url": arweave_url, |
469 | | - "valid": "UNKNOWN", |
470 | | - } |
471 | | - |
472 | | - try: |
473 | | - bag_name = os.listdir(tmp_dir)[0] |
474 | | - bag_file = Path(tmp_dir) / bag_name |
475 | | - except IndexError: |
476 | | - response.status_code = status.HTTP_404_NOT_FOUND |
477 | | - return { |
478 | | - "transaction_url": transaction_url, |
479 | | - "file_url": arweave_url, |
480 | | - "valid": "UNKNOWN", |
481 | | - } |
482 | | - |
483 | | - # Create bag object and validate, and return information from it. |
484 | | - try: |
485 | | - arkly_bag = bagit.Bag(str(bag_file)) |
486 | | - return { |
487 | | - "transaction_url": transaction_url, |
488 | | - "file_url": arweave_url, |
489 | | - "valid": f"{arkly_bag.validate()}", |
490 | | - "bag_info": arkly_bag.info, |
491 | | - "bag_name": bag_name, |
492 | | - } |
493 | | - except bagit.BagError: |
494 | | - response.status_code = status.HTTP_422_UNPROCESSABLE_ENTITY |
495 | | - return { |
496 | | - "transaction_url": transaction_url, |
497 | | - "file_url": arweave_url, |
498 | | - "valid": "UNKNOWN", |
499 | | - } |
500 | | - |
501 | | - |
502 | 410 | async def _all_transactions(wallet_addr: str): |
503 | 411 | """Retrieve all transactions from a given wallet and return a human |
504 | 412 | friendly link to enable users to view the transaction. |
@@ -537,4 +445,4 @@ async def _retrieve_by_tag_pair(name: str, value: str) -> dict: |
537 | 445 |
|
538 | 446 | async def _get_version_info() -> dict: |
539 | 447 | """Return information about the versions used by this API.""" |
540 | | - return {"api": get_version(), "agent": ARKLY_AGENT, "bagit": bagit.VERSION} |
| 448 | + return {"api": get_version()} |
0 commit comments