Skip to content

Commit 4ba1b26

Browse files
author
toddn
committed
changed added from 2.0 WIP - prepare for merge
1 parent 0793274 commit 4ba1b26

File tree

9 files changed

+85
-334
lines changed

9 files changed

+85
-334
lines changed

app/api/Datasets.scala

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class Datasets @Inject()(
4848
userService: UserService,
4949
thumbnailService : ThumbnailService,
5050
appConfig: AppConfigurationService,
51-
esqueue: ElasticsearchQueue,
5251
adminsNotifierService: AdminsNotifierService,
52+
searches: SearchService,
5353
extractionBusService: ExtractionBusService) extends ApiController {
5454

5555
def get(id: UUID) = PermissionAction(Permission.ViewDataset, Some(ResourceRef(ResourceRef.dataset, id))) { implicit request =>
@@ -224,14 +224,9 @@ class Datasets @Inject()(
224224
if (!file.xmlMetadata.isEmpty) {
225225
val xmlToJSON = files.getXMLMetadataJSON(UUID(file_id))
226226
datasets.addXMLMetadata(UUID(id), UUID(file_id), xmlToJSON)
227-
current.plugin[ElasticsearchPlugin].foreach {
228-
_.index(SearchUtils.getElasticsearchObject(d))
229227
}
230-
} else {
231-
current.plugin[ElasticsearchPlugin].foreach {
232-
_.index(SearchUtils.getElasticsearchObject(d))
233-
}
234-
}
228+
searches.index(d, true)
229+
235230

236231
adminsNotifierService.sendAdminsNotification(Utils.baseUrl(request), "Dataset", "added", id, name)
237232

@@ -390,9 +385,8 @@ class Datasets @Inject()(
390385
def reindex(id: UUID, recursive: Boolean) = PermissionAction(Permission.CreateDataset, Some(ResourceRef(ResourceRef.dataset, id))) { implicit request =>
391386
datasets.get(id) match {
392387
case Some(ds) => {
393-
val success = esqueue.queue("index_dataset", new ResourceRef('dataset, id), new ElasticsearchParameters(recursive=recursive))
394-
if (success) Ok(toJson(Map("status" -> "reindex successfully queued")))
395-
else BadRequest(toJson(Map("status" -> "reindex queuing failed, Elasticsearch may be disabled")))
388+
datasets.index(id)
389+
Ok(toJson(s"Dataset $id reindexed"))
396390
}
397391
case None => {
398392
Logger.error("Error getting dataset" + id)
@@ -1617,16 +1611,8 @@ class Datasets @Inject()(
16171611
}
16181612

16191613
def jsonPreview(pvId: String, pId: String, pPath: String, pMain: String, pvRoute: String, pvContentType: String, pvLength: Long): JsValue = {
1620-
if (pId.equals("X3d"))
1621-
toJson(Map("pv_id" -> pvId, "p_id" -> pId,
1622-
"p_path" -> controllers.routes.Assets.at(pPath).toString,
1623-
"p_main" -> pMain, "pv_route" -> pvRoute,
1624-
"pv_contenttype" -> pvContentType, "pv_length" -> pvLength.toString,
1625-
"pv_annotationsEditPath" -> api.routes.Previews.editAnnotation(UUID(pvId)).toString,
1626-
"pv_annotationsListPath" -> api.routes.Previews.listAnnotations(UUID(pvId)).toString,
1627-
"pv_annotationsAttachPath" -> api.routes.Previews.attachAnnotation(UUID(pvId)).toString))
1628-
else
1629-
toJson(Map("pv_id" -> pvId, "p_id" -> pId, "p_path" -> controllers.routes.Assets.at(pPath).toString, "p_main" -> pMain, "pv_route" -> pvRoute, "pv_contenttype" -> pvContentType, "pv_length" -> pvLength.toString))
1614+
toJson(Map("pv_id" -> pvId, "p_id" -> pId, "p_path" -> controllers.routes.Assets.at(pPath).toString,
1615+
"p_main" -> pMain, "pv_route" -> pvRoute, "pv_contenttype" -> pvContentType, "pv_length" -> pvLength.toString))
16301616
}
16311617

16321618
def getPreviews(id: UUID) = PermissionAction(Permission.ViewDataset, Some(ResourceRef(ResourceRef.dataset, id))) { implicit request =>

app/api/Files.scala

Lines changed: 8 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.net.{URL, URLEncoder}
66

77
import javax.inject.Inject
88
import javax.mail.internet.MimeUtility
9-
import _root_.util.{FileUtils, JSONLD, Parsers, RequestUtils, SearchUtils}
9+
import _root_.util.{FileUtils, JSONLD, Parsers, RequestUtils}
1010
import com.mongodb.casbah.Imports._
1111
import controllers.Previewers
1212
import jsonutils.JsonUtil
@@ -42,7 +42,6 @@ class Files @Inject()(
4242
extractions: ExtractionService,
4343
dtsrequests:ExtractionRequestsService,
4444
previews: PreviewService,
45-
threeD: ThreeDService,
4645
metadataService: MetadataService,
4746
contextService: ContextLDService,
4847
thumbnails: ThumbnailService,
@@ -53,7 +52,8 @@ class Files @Inject()(
5352
appConfig: AppConfigurationService,
5453
adminsNotifierService: AdminsNotifierService,
5554
extractionBusService: ExtractionBusService,
56-
esqueue: ElasticsearchQueue) extends ApiController {
55+
versusService: VersusService,
56+
searches: SearchService) extends ApiController {
5757

5858
def get(id: UUID) = PermissionAction(Permission.ViewFile, Some(ResourceRef(ResourceRef.file, id))) { implicit request =>
5959
Logger.debug("GET file with id " + id)
@@ -560,9 +560,8 @@ class Files @Inject()(
560560
def reindex(id: UUID) = PermissionAction(Permission.AddFile, Some(ResourceRef(ResourceRef.file, id))) { implicit request =>
561561
files.get(id) match {
562562
case Some(file) => {
563-
val success = esqueue.queue("index_file", new ResourceRef('file, id))
564-
if (success) Ok(toJson(Map("status" -> "reindex successfully queued")))
565-
else BadRequest(toJson(Map("status" -> "reindex queuing failed, Elasticsearch may be disabled")))
563+
files.index(id)
564+
Ok(toJson(s"File $id reindexed"))
566565
}
567566
case None => {
568567
Logger.error("Error getting file" + id)
@@ -771,52 +770,6 @@ class Files @Inject()(
771770
}
772771
}
773772

774-
/**
775-
* Add 3D geometry file to file.
776-
*/
777-
def attachGeometry(file_id: UUID, geometry_id: UUID) = PermissionAction(Permission.AddFile, Some(ResourceRef(ResourceRef.file, file_id)))(parse.json) { implicit request =>
778-
request.body match {
779-
case JsObject(fields) => {
780-
files.get(file_id) match {
781-
case Some(file) => {
782-
threeD.getGeometry(geometry_id) match {
783-
case Some(geometry) =>
784-
threeD.updateGeometry(file_id, geometry_id, fields)
785-
Ok(toJson(Map("status" -> "success")))
786-
case None => BadRequest(toJson("Geometry file not found"))
787-
}
788-
}
789-
case None => BadRequest(toJson("File not found " + file_id))
790-
}
791-
}
792-
case _ => Ok("received something else: " + request.body + '\n')
793-
}
794-
}
795-
796-
797-
/**
798-
* Add 3D texture to file.
799-
*/
800-
def attachTexture(file_id: UUID, texture_id: UUID) = PermissionAction(Permission.AddFile, Some(ResourceRef(ResourceRef.file, file_id)))(parse.json) { implicit request =>
801-
request.body match {
802-
case JsObject(fields) => {
803-
files.get((file_id)) match {
804-
case Some(file) => {
805-
threeD.getTexture(texture_id) match {
806-
case Some(texture) => {
807-
threeD.updateTexture(file_id, texture_id, fields)
808-
Ok(toJson(Map("status" -> "success")))
809-
}
810-
case None => BadRequest(toJson("Texture file not found"))
811-
}
812-
}
813-
case None => BadRequest(toJson("File not found " + file_id))
814-
}
815-
}
816-
case _ => Ok("received something else: " + request.body + '\n')
817-
}
818-
}
819-
820773
/**
821774
* Add thumbnail to file.
822775
*/
@@ -871,113 +824,6 @@ class Files @Inject()(
871824
}
872825
}
873826

874-
875-
/**
876-
* Find geometry file for given 3D file and geometry filename.
877-
*/
878-
def getGeometry(three_d_file_id: UUID, filename: String) = PermissionAction(Permission.ViewFile, Some(ResourceRef(ResourceRef.file, three_d_file_id))) { implicit request =>
879-
threeD.findGeometry(three_d_file_id, filename) match {
880-
case Some(geometry) => {
881-
882-
threeD.getGeometryBlob(geometry.id) match {
883-
884-
case Some((inputStream, filename, contentType, contentLength)) => {
885-
request.headers.get(RANGE) match {
886-
case Some(value) => {
887-
val range: (Long, Long) = value.substring("bytes=".length).split("-") match {
888-
case x if x.length == 1 => (x.head.toLong, contentLength - 1)
889-
case x => (x(0).toLong, x(1).toLong)
890-
}
891-
range match {
892-
case (start, end) =>
893-
894-
inputStream.skip(start)
895-
import play.api.mvc.{ResponseHeader, SimpleResult}
896-
SimpleResult(
897-
header = ResponseHeader(PARTIAL_CONTENT,
898-
Map(
899-
CONNECTION -> "keep-alive",
900-
ACCEPT_RANGES -> "bytes",
901-
CONTENT_RANGE -> "bytes %d-%d/%d".format(start, end, contentLength),
902-
CONTENT_LENGTH -> (end - start + 1).toString,
903-
CONTENT_TYPE -> contentType
904-
)
905-
),
906-
body = Enumerator.fromStream(inputStream)
907-
)
908-
}
909-
}
910-
case None => {
911-
//IMPORTANT: Setting CONTENT_LENGTH header here introduces bug!
912-
Ok.chunked(Enumerator.fromStream(inputStream))
913-
.withHeaders(CONTENT_TYPE -> contentType)
914-
.withHeaders(CONTENT_DISPOSITION -> (FileUtils.encodeAttachment(filename, request.headers.get("user-agent").getOrElse(""))))
915-
916-
}
917-
}
918-
}
919-
case None => Logger.error("No geometry file found: " + geometry.id); InternalServerError("No geometry file found")
920-
921-
}
922-
}
923-
case None => Logger.error("Geometry file not found"); InternalServerError
924-
}
925-
}
926-
927-
928-
/**
929-
* Find texture file for given 3D file and texture filename.
930-
*/
931-
def getTexture(three_d_file_id: UUID, filename: String) = PermissionAction(Permission.ViewFile, Some(ResourceRef(ResourceRef.file, three_d_file_id))) { implicit request =>
932-
threeD.findTexture(three_d_file_id, filename) match {
933-
case Some(texture) => {
934-
935-
threeD.getBlob(texture.id) match {
936-
937-
case Some((inputStream, filename, contentType, contentLength)) => {
938-
request.headers.get(RANGE) match {
939-
case Some(value) => {
940-
val range: (Long, Long) = value.substring("bytes=".length).split("-") match {
941-
case x if x.length == 1 => (x.head.toLong, contentLength - 1)
942-
case x => (x(0).toLong, x(1).toLong)
943-
}
944-
range match {
945-
case (start, end) =>
946-
947-
inputStream.skip(start)
948-
949-
SimpleResult(
950-
header = ResponseHeader(PARTIAL_CONTENT,
951-
Map(
952-
CONNECTION -> "keep-alive",
953-
ACCEPT_RANGES -> "bytes",
954-
CONTENT_RANGE -> "bytes %d-%d/%d".format(start, end, contentLength),
955-
CONTENT_LENGTH -> (end - start + 1).toString,
956-
CONTENT_TYPE -> contentType
957-
)
958-
),
959-
body = Enumerator.fromStream(inputStream)
960-
)
961-
}
962-
}
963-
case None => {
964-
//IMPORTANT: Setting CONTENT_LENGTH header here introduces bug!
965-
Ok.chunked(Enumerator.fromStream(inputStream))
966-
.withHeaders(CONTENT_TYPE -> contentType)
967-
//.withHeaders(CONTENT_LENGTH -> contentLength.toString)
968-
.withHeaders(CONTENT_DISPOSITION -> (FileUtils.encodeAttachment(filename, request.headers.get("user-agent").getOrElse(""))))
969-
970-
}
971-
}
972-
}
973-
case None => Logger.error("No texture file found: " + texture.id.toString()); InternalServerError("No texture found")
974-
975-
}
976-
}
977-
case None => Logger.error("Texture file not found"); InternalServerError
978-
}
979-
}
980-
981827
/**
982828
* REST endpoint: PUT: update or change the filename
983829
* args
@@ -1445,13 +1291,6 @@ class Files @Inject()(
14451291
}
14461292

14471293
def jsonPreview(pvId: UUID, pId: String, pPath: String, pMain: String, pvRoute: java.lang.String, pvContentType: String, pvLength: Long): JsValue = {
1448-
if (pId.equals("X3d"))
1449-
toJson(Map("pv_id" -> pvId.stringify, "p_id" -> pId, "p_path" -> controllers.routes.Assets.at(pPath).toString,
1450-
"p_main" -> pMain, "pv_route" -> pvRoute, "pv_contenttype" -> pvContentType, "pv_length" -> pvLength.toString,
1451-
"pv_annotationsEditPath" -> api.routes.Previews.editAnnotation(pvId).toString,
1452-
"pv_annotationsListPath" -> api.routes.Previews.listAnnotations(pvId).toString,
1453-
"pv_annotationsAttachPath" -> api.routes.Previews.attachAnnotation(pvId).toString))
1454-
else
14551294
toJson(Map("pv_id" -> pvId.stringify, "p_id" -> pId, "p_path" -> controllers.routes.Assets.at(pPath).toString,
14561295
"p_main" -> pMain, "pv_route" -> pvRoute, "pv_contenttype" -> pvContentType, "pv_length" -> pvLength.toString))
14571296
}
@@ -1600,17 +1439,11 @@ class Files @Inject()(
16001439

16011440
//this stmt has to be before files.removeFile
16021441
Logger.debug("Deleting file from indexes " + file.filename)
1603-
current.plugin[VersusPlugin].foreach {
1604-
_.removeFromIndexes(id)
1605-
}
1442+
versusService.removeFromIndexes(id)
16061443
Logger.debug("Deleting file: " + file.filename)
16071444
files.removeFile(id, Utils.baseUrl(request), request.apiKey, request.user)
1608-
appConfig.incrementCount('files, -1)
1609-
appConfig.incrementCount('bytes, -file.length)
16101445

1611-
current.plugin[ElasticsearchPlugin].foreach {
1612-
_.delete("data", "file", id.stringify)
1613-
}
1446+
searches.delete(id.stringify, "file")
16141447

16151448
adminsNotifierService.sendAdminsNotification(Utils.baseUrl(request), "File", "removed", id.stringify, file.filename)
16161449

@@ -1689,11 +1522,7 @@ class Files @Inject()(
16891522

16901523
def index(id: UUID) {
16911524
files.get(id) match {
1692-
case Some(file) => {
1693-
current.plugin[ElasticsearchPlugin].foreach {
1694-
_.index(SearchUtils.getElasticsearchObject(file))
1695-
}
1696-
}
1525+
case Some(file) => searches.index(file)
16971526
case None => Logger.error("File not found: " + id)
16981527
}
16991528
}

app/api/Indexes.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import services.{ExtractorMessage, _}
1414
* Index data.
1515
*/
1616
@Inject
17-
class Indexes @Inject() (multimediaSearch: MultimediaQueryService, previews: PreviewService, extractionBusService: ExtractionBusService) extends Controller with ApiController {
17+
class Indexes @Inject() (multimediaSearch: MultimediaQueryService, previews: PreviewService, versusService: VersusService, extractionBusService: ExtractionBusService) extends Controller with ApiController {
1818

1919
/**
2020
* Submit section, preview, file for indexing.
@@ -26,9 +26,7 @@ class Indexes @Inject() (multimediaSearch: MultimediaQueryService, previews: Pre
2626
case Some(p) =>
2727
extractionBusService.submitSectionPreviewManually(p, new UUID(section_id), Utils.baseUrl(request), request.apiKey)
2828
val fileType = p.contentType
29-
current.plugin[VersusPlugin].foreach{
30-
_.indexPreview(p.id,fileType)
31-
}
29+
versusService.indexPreview(p.id,fileType)
3230
Ok(toJson("success"))
3331
case None =>
3432
BadRequest(toJson("Missing parameter [preview_id]"))

app/api/Metadata.scala

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Metadata @Inject() (
4040
vocabs: StandardVocabularyService,
4141
events: EventService,
4242
spaceService: SpaceService,
43+
searches: SearchService,
4344
extractionBusService: ExtractionBusService) extends ApiController {
4445

4546
def getDefinitions() = PermissionAction(Permission.ViewDataset) {
@@ -71,22 +72,19 @@ class Metadata @Inject() (
7172
}
7273

7374
// Next get Elasticsearch metadata fields if plugin available
74-
current.plugin[ElasticsearchPlugin] match {
75-
case Some(plugin) => {
76-
val mdTerms = plugin.getAutocompleteMetadataFields(query)
75+
if (searches.isEnabled()) {
76+
val mdTerms = searches.getAutocompleteMetadataFields(query)
7777
for (term <- mdTerms) {
7878
// e.g. "metadata.http://localhost:9000/clowder/api/extractors/terra.plantcv.angle",
7979
// "metadata.Jane Doe.Alternative Title"
8080
if (!(listOfTerms contains term))
8181
listOfTerms.append(term)
8282
}
8383
Ok(toJson(listOfTerms.distinct))
84-
}
85-
case None => {
84+
} else {
8685
BadRequest("Elasticsearch plugin is not enabled")
8786
}
8887
}
89-
}
9088

9189
def getDefinitionsByDataset(id: UUID) = PermissionAction(Permission.AddMetadata, Some(ResourceRef(ResourceRef.dataset, id))) { implicit request =>
9290
implicit val user = request.user
@@ -548,20 +546,15 @@ class Metadata @Inject() (
548546

549547
m.attachedTo.resourceType match {
550548
case ResourceRef.file => {
551-
current.plugin[ElasticsearchPlugin].foreach { p =>
552-
// Delete existing index entry and re-index
553-
p.delete(m.attachedTo.id.stringify)
554-
files.index(m.attachedTo.id)
555-
}
549+
searches.delete(m.attachedTo.id.stringify)
550+
files.index(m.attachedTo.id)
556551
extractionBusService.metadataRemovedFromResource(id, m.attachedTo, Utils.baseUrl(request),
557552
request.apiKey, request.user)
558553
}
559554
case ResourceRef.dataset => {
560-
current.plugin[ElasticsearchPlugin].foreach { p =>
561-
// Delete existing index entry and re-index
562-
p.delete(m.attachedTo.id.stringify)
563-
datasets.index(m.attachedTo.id)
564-
}
555+
// Delete existing index entry and re-index
556+
searches.delete(m.attachedTo.id.stringify)
557+
datasets.index(m.attachedTo.id)
565558
extractionBusService.metadataRemovedFromResource(id, m.attachedTo, Utils.baseUrl(request), request.apiKey, request.user)
566559
}
567560
case _ => {

0 commit comments

Comments
 (0)