@@ -5,16 +5,18 @@ import java.net.URL
55import java .security .{DigestInputStream , MessageDigest }
66import java .text .SimpleDateFormat
77import java .util .{Calendar , Date }
8+
89import api .Permission .Permission
910import java .util .zip ._
11+
1012import javax .inject .{Inject , Singleton }
1113import controllers .{Previewers , Utils }
1214import jsonutils .JsonUtil
1315import models ._
1416import org .apache .commons .codec .binary .Hex
1517import org .json .JSONObject
1618import play .api .Logger
17- import play .api .Play .{configuration , current }
19+ import play .api .Play .{configuration , current , routes }
1820import play .api .i18n .Messages
1921import play .api .libs .concurrent .Execution .Implicits ._
2022import play .api .libs .iteratee .Enumerator
@@ -23,6 +25,8 @@ import play.api.libs.json.Json._
2325import play .api .mvc .AnyContent
2426import services ._
2527import _root_ .util ._
28+ import controllers .Utils .https
29+
2630import scala .concurrent .{ExecutionContext , Future }
2731import scala .collection .mutable .ListBuffer
2832
@@ -2066,14 +2070,15 @@ class Datasets @Inject()(
20662070 * @param chunkSize chunk size in memory in which to buffer the stream
20672071 * @param compression java built in compression value. Use 0 for no compression.
20682072 * @param bagit whether or not to include bagit structures in zip
2073+ * @param baseURL the root Clowder URL for metadata files, from original request
20692074 * @param user an optional user to include in metadata
20702075 * @param fileIDs a list of UUIDs of files in the dataset to include (i.e. marked file downloads)
20712076 * @param folderId a folder UUID in the dataset to include (i.e. folder download)
20722077 * @return Enumerator to produce array of bytes from a zipped stream containing the bytes of each file
20732078 * in the dataset
20742079 */
20752080 def enumeratorFromDataset (dataset : Dataset , chunkSize : Int = 1024 * 8 ,
2076- compression : Int = Deflater .DEFAULT_COMPRESSION , bagit : Boolean ,
2081+ compression : Int = Deflater .DEFAULT_COMPRESSION , bagit : Boolean , baseURL : String ,
20772082 user : Option [User ], fileIDs : Option [List [UUID ]], folderId : Option [UUID ])
20782083 (implicit ec : ExecutionContext ): Enumerator [Array [Byte ]] = {
20792084 implicit val pec = ec.prepare()
@@ -2193,7 +2198,7 @@ class Datasets @Inject()(
21932198 }
21942199 case (" bag" , " datacite.xml" ) => {
21952200 // RDA-recommended DataCite xml file
2196- is = addDataCiteMetadataToZip(zip, dataset)
2201+ is = addDataCiteMetadataToZip(zip, dataset, baseURL )
21972202 file_type = " tagmanifest-md5.txt"
21982203 }
21992204 case (" bag" , " tagmanifest-md5.txt" ) => {
@@ -2359,7 +2364,7 @@ class Datasets @Inject()(
23592364 Some (new ByteArrayInputStream (s.getBytes(" UTF-8" )))
23602365 }
23612366
2362- private def addDataCiteMetadataToZip (zip : ZipOutputStream , dataset : Dataset ): Option [InputStream ] = {
2367+ private def addDataCiteMetadataToZip (zip : ZipOutputStream , dataset : Dataset , baseURL : String ): Option [InputStream ] = {
23632368 zip.putNextEntry(new ZipEntry (" metadata/datacite.xml" ))
23642369 var s = " <resource xsi:schemaLocation=\" http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4/metadata.xsd\" >\n "
23652370 // https://support.datacite.org/docs/schema-40
@@ -2451,9 +2456,8 @@ class Datasets @Inject()(
24512456 s += " </subjects>\n "
24522457 }
24532458
2454- // RelatedIdentifier
2455- val clowder_url = " CLOWDER URL GOES HERE"
2456- s += " <relatedIdentifier relatedIdentifierType=\" URL\" relationType=\" isSourceOf\" >" + clowder_url+ " </ResourceType>\n "
2459+ // RelatedIdentifier (Clowder root URL)
2460+ s += " <relatedIdentifier relatedIdentifierType=\" URL\" relationType=\" isSourceOf\" >" + baseURL+ " </ResourceType>\n "
24572461
24582462 // Size (can have many)
24592463 s += " <sizes>\n\t <size>" + dataset.files.length.toString+ " files</size>\n </sizes>\n "
@@ -2493,14 +2497,15 @@ class Datasets @Inject()(
24932497 datasets.get(id) match {
24942498 case Some (dataset) => {
24952499 val bagit = play.api.Play .configuration.getBoolean(" downloadDatasetBagit" ).getOrElse(true )
2500+ val baseURL = controllers.routes.Datasets .dataset(id).absoluteURL(https(request))
24962501
24972502 // Increment download count if tracking is enabled
24982503 if (tracking)
24992504 datasets.incrementDownloads(id, user)
25002505
25012506 // Use custom enumerator to create the zip file on the fly
25022507 // Use a 1MB in memory byte array
2503- Ok .chunked(enumeratorFromDataset(dataset,1024 * 1024 , compression, bagit, user, None , None )).withHeaders(
2508+ Ok .chunked(enumeratorFromDataset(dataset,1024 * 1024 , compression, bagit, baseURL, user, None , None )).withHeaders(
25042509 CONTENT_TYPE -> " application/zip" ,
25052510 CONTENT_DISPOSITION -> (FileUtils .encodeAttachment(dataset.name+ " .zip" , request.headers.get(" user-agent" ).getOrElse(" " )))
25062511 )
@@ -2519,13 +2524,14 @@ class Datasets @Inject()(
25192524 case Some (dataset) => {
25202525 val fileIDs = fileList.split(',' ).map(fid => new UUID (fid)).toList
25212526 val bagit = play.api.Play .configuration.getBoolean(" downloadDatasetBagit" ).getOrElse(true )
2527+ val baseURL = controllers.routes.Datasets .dataset(id).absoluteURL(https(request))
25222528
25232529 // Increment download count for each file
25242530 fileIDs.foreach(fid => files.incrementDownloads(fid, user))
25252531
25262532 // Use custom enumerator to create the zip file on the fly
25272533 // Use a 1MB in memory byte array
2528- Ok .chunked(enumeratorFromDataset(dataset,1024 * 1024 , - 1 , bagit, user, Some (fileIDs), None )).withHeaders(
2534+ Ok .chunked(enumeratorFromDataset(dataset,1024 * 1024 , - 1 , bagit, baseURL, user, Some (fileIDs), None )).withHeaders(
25292535 CONTENT_TYPE -> " application/zip" ,
25302536 CONTENT_DISPOSITION -> (FileUtils .encodeAttachment(dataset.name+ " (Partial).zip" , request.headers.get(" user-agent" ).getOrElse(" " )))
25312537 )
@@ -2543,6 +2549,8 @@ class Datasets @Inject()(
25432549 datasets.get(id) match {
25442550 case Some (dataset) => {
25452551 val bagit = play.api.Play .configuration.getBoolean(" downloadDatasetBagit" ).getOrElse(true )
2552+ val baseURL = controllers.routes.Datasets .dataset(id).absoluteURL(https(request))
2553+
25462554
25472555 // Increment download count for each file in folder
25482556 folders.get(folderId) match {
@@ -2551,7 +2559,7 @@ class Datasets @Inject()(
25512559
25522560 // Use custom enumerator to create the zip file on the fly
25532561 // Use a 1MB in memory byte array
2554- Ok .chunked(enumeratorFromDataset(dataset,1024 * 1024 , - 1 , bagit, user, None , Some (folderId))).withHeaders(
2562+ Ok .chunked(enumeratorFromDataset(dataset,1024 * 1024 , - 1 , bagit, baseURL, user, None , Some (folderId))).withHeaders(
25552563 CONTENT_TYPE -> " application/zip" ,
25562564 CONTENT_DISPOSITION -> (FileUtils .encodeAttachment(dataset.name+ " (" + fo.name+ " Folder).zip" , request.headers.get(" user-agent" ).getOrElse(" " )))
25572565 )
0 commit comments