diff --git a/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala b/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala index df9047ff832b40e2a76a2eafd6d53f0160540276..fabc95d43856ceabc371f9baa46ae51a38b20c7d 100644 --- a/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala +++ b/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala @@ -144,7 +144,6 @@ class ContentActor @Inject() (implicit oec: OntologyEngineContext, ss: StorageSe def uploadPreSignedUrl(request: Request): Future[Response] = { val `type`: String = request.get("type").asInstanceOf[String].toLowerCase() val fileName: String = request.get("fileName").asInstanceOf[String] - val mimeType: String = request.get("mimeType").asInstanceOf[String] val filePath: String = request.getRequest.getOrDefault("filePath","").asInstanceOf[String] .replaceAll("^/+|/+$", "") val identifier: String = request.get("identifier").asInstanceOf[String] @@ -153,7 +152,7 @@ class ContentActor @Inject() (implicit oec: OntologyEngineContext, ss: StorageSe val objectKey = if (StringUtils.isEmpty(filePath)) "content" + File.separator + `type` + File.separator + identifier + File.separator + Slug.makeSlug(fileName, true) else filePath + File.separator + "content" + File.separator + `type` + File.separator + identifier + File.separator + Slug.makeSlug(fileName, true) val expiry = Platform.config.getString("cloud_storage.upload.url.ttl") - val preSignedURL = ss.getSignedURL(objectKey, Option.apply(expiry.toInt), Option.apply("w"), Option.apply(mimeType)) + val preSignedURL = ss.getSignedURL(objectKey, Option.apply(expiry.toInt), Option.apply("w")) ResponseHandler.OK().put("identifier", identifier).put("pre_signed_url", preSignedURL) .put("url_expiry", expiry) }) recoverWith { case e: CompletionException => throw e.getCause } diff --git a/content-api/content-actors/src/test/scala/org/sunbird/content/actors/TestContentActor.scala b/content-api/content-actors/src/test/scala/org/sunbird/content/actors/TestContentActor.scala index 52b413a438ba0f7a2c9ce71c976c8e364cc24cf9..5c15e56d2499c525b837c32a7ff2ddcf346de031 100644 --- a/content-api/content-actors/src/test/scala/org/sunbird/content/actors/TestContentActor.scala +++ b/content-api/content-actors/src/test/scala/org/sunbird/content/actors/TestContentActor.scala @@ -94,7 +94,7 @@ class TestContentActor extends BaseSpec with MockFactory { (oec.graphService _).expects().returns(graphDB) (graphDB.getNodeByUniqueId(_: String, _: String, _: Boolean, _: Request)).expects(*, *, *, *).returns(Future(getValidNode())) implicit val ss: StorageService = mock[StorageService] - (ss.getSignedURL(_: String, _: Option[Int], _: Option[String], _: Option[String])).expects(*, *, *, *).returns("cloud store url") + (ss.getSignedURL(_: String, _: Option[Int], _: Option[String])).expects(*, *, *).returns("cloud store url") val request = getContentRequest() request.getRequest.putAll(mapAsJavaMap(Map("fileName" -> "presigned_url", "filePath" -> "/data/cloudstore/", "type" -> "assets", "identifier" -> "do_1234"))) request.setOperation("uploadPreSignedUrl") diff --git a/platform-modules/mimetype-manager/src/main/scala/org/sunbird/cloudstore/StorageService.scala b/platform-modules/mimetype-manager/src/main/scala/org/sunbird/cloudstore/StorageService.scala index f03a79627b7385c7d7e95997d979e3871c07e538..3904888a09ad8c58c82d9a75aa4432ece5d0eab5 100644 --- a/platform-modules/mimetype-manager/src/main/scala/org/sunbird/cloudstore/StorageService.scala +++ b/platform-modules/mimetype-manager/src/main/scala/org/sunbird/cloudstore/StorageService.scala @@ -1,14 +1,13 @@ package org.sunbird.cloudstore -import java.io.File import org.apache.commons.lang3.StringUtils +import org.apache.tika.Tika import org.sunbird.cloud.storage.BaseStorageService -import org.sunbird.common.Platform -import org.sunbird.cloud.storage.factory.StorageConfig -import org.sunbird.cloud.storage.factory.StorageServiceFactory +import org.sunbird.cloud.storage.factory.{StorageConfig, StorageServiceFactory} +import org.sunbird.common.{Platform, Slug} import org.sunbird.common.exception.ServerException -import org.sunbird.common.Slug +import java.io.File import scala.concurrent.{ExecutionContext, Future} class StorageService { @@ -85,9 +84,9 @@ class StorageService { getService.deleteObject(getContainerName, key, isDirectory) } - def getSignedURL(key: String, ttl: Option[Int], permission: Option[String], contentType: Option[String] = None): String = { + def getSignedURL(key: String, ttl: Option[Int], permission: Option[String]): String = { storageType match { - case "gcloud" => getService.getPutSignedURL(getContainerName, key, ttl, permission, contentType) + case "gcloud" => getService.getPutSignedURL(getContainerName, key, ttl, permission, Option.apply(getMimeType(key))) case _ => getService.getSignedURL (getContainerName, key, ttl, permission) } } @@ -101,4 +100,10 @@ class StorageService { "" } } + + def getMimeType(fileName: String): String = { + val tika: Tika = new Tika() + tika.detect(fileName) + } + } diff --git a/platform-modules/mimetype-manager/src/test/scala/org/sunbird/cloudstore/StorageServiceTest.scala b/platform-modules/mimetype-manager/src/test/scala/org/sunbird/cloudstore/StorageServiceTest.scala index 47ba320e1a094210c5fff3055ee960a95bce5e7c..071a9e20cf55f9a58c4619182a57af46d54668dc 100644 --- a/platform-modules/mimetype-manager/src/test/scala/org/sunbird/cloudstore/StorageServiceTest.scala +++ b/platform-modules/mimetype-manager/src/test/scala/org/sunbird/cloudstore/StorageServiceTest.scala @@ -19,7 +19,7 @@ class StorageServiceTest extends AsyncFlatSpec with Matchers { "getSignedURL" should "return the signed url" in { val objectKey = "content" + File.separator + "asset" + File.separator + "do_53245" + File.separator + "abc.png" - val preSignedURL = ss.getSignedURL(objectKey, Option.apply(600), Option.apply("w"), Option.apply("")) + val preSignedURL = ss.getSignedURL(objectKey, Option.apply(600), Option.apply("w")) assert(preSignedURL.contains(objectKey)) } @@ -27,4 +27,29 @@ class StorageServiceTest extends AsyncFlatSpec with Matchers { val uri = ss.getUri("content/abc.json") assert(uri != null) } + + "getMimeType" should "return the mimetype application/epub+zip for epub" in { + val result = ss.getMimeType("test.alert.epub") + assert(result == "application/epub+zip") + } + + "getMimeType" should "return the mimetype application/octet-stream for h5p" in { + val result = ss.getMimeType("test.alert.h5p") + assert(result == "application/octet-stream") + } + + "getMimeType" should "return the mimetype text/csv for csv" in { + val result = ss.getMimeType("test.alert.csv") + assert(result == "text/csv") + } + + "getMimeType" should "return the mimetype application/pdf for pdf" in { + val result = ss.getMimeType("test.alert.pdf") + assert(result == "application/pdf") + } + + "getMimeType" should "return the mimetype application/zip for zip" in { + val result = ss.getMimeType("test.alert.zip") + assert(result == "application/zip") + } }