Skip to content

Commit

Permalink
fix all get() logic -- only use for has_trusted_url (new field)
Browse files Browse the repository at this point in the history
  • Loading branch information
goose-life committed Sep 26, 2024
1 parent 890772b commit a5e1c29
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions peachjam/adapters/indigo.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ def download_source_file(self, url, doc, title):
},
)

def get_size_from_url(self, url):
logger.info(" Getting the file size ...")
r = self.client_get(url)
return len(r.content)

def create_publication_file(self, publication_document, doc, title, stub=False):
from peachjam.models import PublicationFile

Expand Down Expand Up @@ -558,12 +563,8 @@ def create_publication_file(self, publication_document, doc, title, stub=False):
)
if publication_document.get("has_trusted_url"):
logger.info(f" Using publication file from trusted URL {url}")
mimetype = publication_document.get("mime_type", "application/pdf")
size = publication_document.get("size")
if not size:
logger.info(" Getting the file size ...")
r = self.client_get(url)
size = len(r.content)
mimetype = publication_document["mime_type"] or "application/pdf"
size = publication_document["size"] or self.get_size_from_url(url)
logger.info(f" Size is {size}")
PublicationFile.objects.update_or_create(
document=doc,
Expand All @@ -581,8 +582,8 @@ def create_publication_file(self, publication_document, doc, title, stub=False):
with NamedTemporaryFile() as f:
r = self.client_get(url)
f.write(r.content)
mimetype = publication_document.get(
"mime_type", magic.from_file(f.name, mime=True)
mimetype = publication_document["mime_type"] or magic.from_file(
f.name, mime=True
)
file = File(f, name=filename)
PublicationFile.objects.update_or_create(
Expand Down

0 comments on commit a5e1c29

Please sign in to comment.