@@ -48,6 +48,7 @@ class Files @Inject() (
4848 contextLDService : ContextLDService ,
4949 spaces : SpaceService ,
5050 folders : FolderService ,
51+ fileConvertService : FileConvertService ,
5152 versusService : VersusService ,
5253 adminsNotifierService : AdminsNotifierService ,
5354 appConfig : AppConfigurationService ,
@@ -219,39 +220,31 @@ class Files @Inject() (
219220 }
220221 }
221222
222- // call Polyglot to get all possible output formats for this file's content type
223- current.plugin[PolyglotPlugin ] match {
224- case Some (plugin) => {
225- Logger .debug(" Polyglot plugin found" )
226-
227- // Increment view count for file
228- val (view_count, view_date) = files.incrementViews(id, user)
229-
230- val fname = file.filename
231- // use name of the file to get the extension (pdf or txt or jpg) to use an input type for Polyglot
232- val lastDividerIndex = (fname.replace(" /" , " ." ).lastIndexOf(" ." )) + 1
233- // drop all elements left of last divider index
234- val contentTypeEnding = fname.drop(lastDividerIndex)
235- Logger .debug(" file name ends in " + contentTypeEnding)
236- // get output formats from Polyglot plugin and pass as the last parameter to view
237- plugin.getOutputFormats(contentTypeEnding).map(outputFormats =>
238- Ok (views.html.file(file, id.stringify, commentsByFile, previewsWithPreviewer, sectionsWithPreviews,
239- extractorsActive, decodedDatasetsContaining.toList, foldersContainingFile,
240- mds, extractionGroups, outputFormats, space, access, folderHierarchy.reverse.toList, decodedSpacesContaining.toList, allDecodedDatasets.toList, view_count, view_date)))
241- }
242- case None =>
243- Logger .debug(" Polyglot plugin not found" )
244223
245- // Increment view count for file
246- val (view_count, view_date) = files.incrementViews(id, user)
224+ // Increment view count for file
225+ val (view_count, view_date) = files.incrementViews(id, user)
226+
227+ val fname = file.filename
228+ // use name of the file to get the extension (pdf or txt or jpg) to use an input type for Polyglot
229+ val lastDividerIndex = (fname.replace(" /" , " ." ).lastIndexOf(" ." )) + 1
230+ // drop all elements left of last divider index
231+ val contentTypeEnding = fname.drop(lastDividerIndex)
232+ Logger .debug(" file name ends in " + contentTypeEnding)
233+
234+ // call FileTransferService to get all possible output formats for this file's content type
247235
248- // passing None as the last parameter (list of output formats)
249- Future (Ok (views.html.file(file, id.stringify, commentsByFile, previewsWithPreviewer, sectionsWithPreviews,
236+ if (play.Play .application().configuration().getBoolean(" fileconvertService" )) {
237+ // get output formats from Polyglot plugin and pass as the last parameter to view
238+ fileConvertService.getOutputFormats(contentTypeEnding).map(outputFormats =>
239+ Ok (views.html.file(file, id.stringify, commentsByFile, previewsWithPreviewer, sectionsWithPreviews,
250240 extractorsActive, decodedDatasetsContaining.toList, foldersContainingFile,
251- mds, extractionGroups, None , space, access, folderHierarchy.reverse.toList, decodedSpacesContaining.toList, allDecodedDatasets.toList, view_count, view_date)))
241+ mds, extractionGroups, outputFormats, space, access, folderHierarchy.reverse.toList, decodedSpacesContaining.toList, allDecodedDatasets.toList, view_count, view_date)))
242+ } else {
243+ Future (Ok (views.html.file(file, id.stringify, commentsByFile, previewsWithPreviewer, sectionsWithPreviews,
244+ extractorsActive, decodedDatasetsContaining.toList, foldersContainingFile,
245+ mds, extractionGroups, None , space, access, folderHierarchy.reverse.toList, decodedSpacesContaining.toList, allDecodedDatasets.toList, view_count, view_date)))
252246 }
253247 }
254-
255248 case None => {
256249 val error_str = s " The file with id ${id} is not found. "
257250 Logger .error(error_str)
@@ -738,81 +731,73 @@ class Files @Inject() (
738731 */
739732 def downloadAsFormat (id : UUID , outputFormat : String ) = PermissionAction (Permission .DownloadFiles , Some (ResourceRef (ResourceRef .file, id))).async { implicit request =>
740733 implicit val user = request.user
734+ if (UUID .isValid(id.stringify)) {
735+ files.get(id) match {
736+ case Some (file) => {
737+ // get bytes for file to be converted
738+ files.getBytes(id) match {
739+ case Some ((inputStream, filename, contentType, contentLength)) => {
741740
742- current.plugin[PolyglotPlugin ] match {
743- case Some (plugin) => {
744- if (UUID .isValid(id.stringify)) {
745- files.get(id) match {
746- case Some (file) => {
747- // get bytes for file to be converted
748- files.getBytes(id) match {
749- case Some ((inputStream, filename, contentType, contentLength)) => {
750-
751- // prepare encoded file name for converted file
752- val lastSeparatorIndex = file.filename.replace(" _" , " ." ).lastIndexOf(" ." )
753- val outputFileName = file.filename.substring(0 , lastSeparatorIndex) + " ." + outputFormat
754-
755- // create local temp file to save polyglot output
756- val tempFileName = " temp_converted_file." + outputFormat
757- val tempFile : java.io.File = new java.io.File (tempFileName)
758- tempFile.deleteOnExit()
759-
760- val outputStream : OutputStream = new BufferedOutputStream (new FileOutputStream (tempFile))
761-
762- val polyglotUser : Option [String ] = configuration.getString(" polyglot.username" )
763- val polyglotPassword : Option [String ] = configuration.getString(" polyglot.password" )
764- val polyglotConvertURL : Option [String ] = configuration.getString(" polyglot.convertURL" )
765-
766- if (polyglotConvertURL.isDefined && polyglotUser.isDefined && polyglotPassword.isDefined) {
767- files.incrementDownloads(id, user)
768-
769- // first call to Polyglot to get url of converted file
770- plugin.getConvertedFileURL(filename, inputStream, outputFormat)
771- .flatMap {
772- convertedFileURL =>
773- val triesLeft = 30
774- // Cponverted file is initially empty. Have to wait for Polyglot to finish conversion.
775- // keep checking until file exists or until too many tries
776- // returns future success only if file is found and downloaded
777- plugin.checkForFileAndDownload(triesLeft, convertedFileURL, outputStream)
778- }.map {
779- x =>
780- // successfuly completed future - get here only after polyglotPlugin.getConvertedFileURL is done executing
781- Ok .chunked(Enumerator .fromStream(new FileInputStream (tempFileName)))
782- .withHeaders(CONTENT_TYPE -> " some-content-Type" )
783- .withHeaders(CONTENT_DISPOSITION -> (FileUtils .encodeAttachment(outputFileName, request.headers.get(" user-agent" ).getOrElse(" " ))))
784- }.recover {
785- case t =>
786- Logger .debug(" Failed: " + t.getMessage())
787- BadRequest (" Error: " + t.getMessage())
788- }
789- } // end of if defined config params
790- else {
791- Logger .error(" Could not get configuration parameters." )
792- Future (BadRequest (" Could not get configuration parameters." ))
793- }
794- } // end of case Some
795- case None => {
796- Logger .error(" Error getting file " + id)
797- Future (BadRequest (" File with this id not found" ))
741+ // prepare encoded file name for converted file
742+ val lastSeparatorIndex = file.filename.replace(" _" , " ." ).lastIndexOf(" ." )
743+ val outputFileName = file.filename.substring(0 , lastSeparatorIndex) + " ." + outputFormat
744+
745+ // create local temp file to save polyglot output
746+ val tempFileName = " temp_converted_file." + outputFormat
747+ val tempFile : java.io.File = new java.io.File (tempFileName)
748+ tempFile.deleteOnExit()
749+
750+ val outputStream : OutputStream = new BufferedOutputStream (new FileOutputStream (tempFile))
751+
752+ val fileConvertUser : Option [String ] = configuration.getString(" fileconvert.username" )
753+ val fileConvertPassword : Option [String ] = configuration.getString(" fileconvert.password" )
754+ val fileConvertConvertURL : Option [String ] = configuration.getString(" fileconvert.convertURL" )
755+
756+ if (fileConvertConvertURL.isDefined && fileConvertUser.isDefined && fileConvertPassword.isDefined) {
757+ files.incrementDownloads(id, user)
758+
759+ // first call to Polyglot to get url of converted file
760+ fileConvertService.getConvertedFileURL(filename, inputStream, outputFormat)
761+ .flatMap {
762+ convertedFileURL =>
763+ val triesLeft = 30
764+ // Cponverted file is initially empty. Have to wait for Polyglot to finish conversion.
765+ // keep checking until file exists or until too many tries
766+ // returns future success only if file is found and downloaded
767+ fileConvertService.checkForFileAndDownload(triesLeft, convertedFileURL, outputStream)
768+ }.map {
769+ x =>
770+ // successfuly completed future - get here only after polyglotPlugin.getConvertedFileURL is done executing
771+ Ok .chunked(Enumerator .fromStream(new FileInputStream (tempFileName)))
772+ .withHeaders(CONTENT_TYPE -> " some-content-Type" )
773+ .withHeaders(CONTENT_DISPOSITION -> (FileUtils .encodeAttachment(outputFileName, request.headers.get(" user-agent" ).getOrElse(" " ))))
774+ }.recover {
775+ case t =>
776+ Logger .debug(" Failed: " + t.getMessage())
777+ BadRequest (" Error: " + t.getMessage())
798778 }
779+ } // end of if defined config params
780+ else {
781+ Logger .error(" Could not get configuration parameters." )
782+ Future (BadRequest (" Could not get configuration parameters." ))
799783 }
800- }
784+ } // end of case Some
801785 case None => {
802- // File could not be found
803- Logger .error(s " Error getting the file with id $id. " )
804- Future (BadRequest (" Invalid file ID" ))
786+ Logger .error(" Error getting file " + id)
787+ Future (BadRequest (" File with this id not found" ))
805788 }
806789 }
807- } // end of if (UUID.isValid(id.stringify))
808- else {
809- Logger .error(s " The given id $id is not a valid ObjectId. " )
810- Future (BadRequest (toJson(s " The given id $id is not a valid ObjectId. " )))
811790 }
812- } // end of case Some(plugin)
813- case None =>
814- Logger .debug(" Polyglot plugin not found" )
815- Future (Ok (" Plugin not found" ))
791+ case None => {
792+ // File could not be found
793+ Logger .error(s " Error getting the file with id $id. " )
794+ Future (BadRequest (" Invalid file ID" ))
795+ }
796+ }
797+ } // end of if (UUID.isValid(id.stringify))
798+ else {
799+ Logger .error(s " The given id $id is not a valid ObjectId. " )
800+ Future (BadRequest (toJson(s " The given id $id is not a valid ObjectId. " )))
816801 }
817802 }
818803
0 commit comments