Unverified Commit cf21bb3c authored by vinukumar-vs's avatar vinukumar-vs Committed by GitHub
Browse files

Issue #KN-231 merge: Merge pull request #874 from AmiableAnil/release-4.10.1

Issue #KN-231 feat: Used the tika to detect the mimetype from filename.
Showing with 40 additions and 11 deletions
+40 -11
......@@ -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 }
......
......@@ -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")
......
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)
}
}
......@@ -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")
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment