Skip to content

Commit

Permalink
Merge pull request #158 from PLOS/ENG-983
Browse files Browse the repository at this point in the history
ENG-983 Set download name in Content-Disposition header for lemur
  • Loading branch information
egh authored Jun 7, 2021
2 parents 501106f + 5d52f14 commit dc1206a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion gcsmigrate/enqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def queue_lemur_final(buckets, state_db):
sql = """
SELECT id,
objects.objKey,
objects.downloadName,
checksum
FROM (
SELECT objKey,
Expand All @@ -155,12 +156,13 @@ def queue_lemur_final(buckets, state_db):
cursor.execute(sql, (bucket_id, initial_id, bucket_id))
row = cursor.fetchone()
while row:
(crepo_file_id, obj_key, sha) = row
(crepo_file_id, obj_key, download_name, sha) = row
to_key = f"{obj_key}"
json = {
"bucket": gcs_bucket_name,
"from_key": sha,
"to_key": to_key,
"download_name": download_name
}
yield CLIENT.publish(TOPIC_PATH, encode_json(json), action="copy")
maybe_update_max(state_db, LATEST_CREPO_ID_KEY, crepo_file_id)
Expand Down
3 changes: 2 additions & 1 deletion gcsmigrate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def main(event, context):
bucket_name = json_struct["bucket"]
from_key = json_struct["from_key"]
to_key = json_struct["to_key"]
copy_object(gcs_client, bucket_name, from_key, to_key)
download_name = json_struct.get("download_name")
copy_object(gcs_client, bucket_name, from_key, to_key, download_name)
else:
raise Exception(f"Bad action: {action}.")
4 changes: 3 additions & 1 deletion gcsmigrate/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ def make_db_connection(db_url):
)


def copy_object(gcs_client, bucket_name, from_key, to_key):
def copy_object(gcs_client, bucket_name, from_key, to_key, download_name=None):
bucket = gcs_client.bucket(bucket_name)
source_blob = bucket.blob(from_key)
bucket.copy_blob(source_blob, bucket, to_key)
mimetype = guess_mimetype(to_key)
target_blob = bucket.blob(to_key)
target_blob.content_type = mimetype
if download_name:
target_blob.content_disposition = f"filename=\"{download_name}\""
target_blob.patch()


Expand Down

0 comments on commit dc1206a

Please sign in to comment.