- put captions into temporary file to remove race condition between start of processing and writing contents of file.

This commit is contained in:
Richard Alam 2019-06-06 10:03:54 -07:00
parent 7f92b58140
commit a8b6717c94

View File

@ -4,7 +4,8 @@ import java.io.{ File, FileOutputStream, FileWriter, IOException }
import java.nio.channels.Channels import java.nio.channels.Channels
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.util import java.util
import java.nio.file.{ Paths, Files } import java.nio.file.{ Files, Paths }
import com.google.gson.Gson import com.google.gson.Gson
import org.bigbluebutton.api.domain.RecordingMetadata import org.bigbluebutton.api.domain.RecordingMetadata
import org.bigbluebutton.api2.RecordingServiceGW import org.bigbluebutton.api2.RecordingServiceGW
@ -14,7 +15,6 @@ import scala.xml.{ Elem, PrettyPrinter, XML }
import scala.collection.JavaConverters._ import scala.collection.JavaConverters._
import scala.collection.mutable.{ Buffer, ListBuffer, Map } import scala.collection.mutable.{ Buffer, ListBuffer, Map }
import scala.collection.Iterable import scala.collection.Iterable
import java.io.IOException import java.io.IOException
import java.nio.charset.Charset import java.nio.charset.Charset
import java.nio.file.Files import java.nio.file.Files
@ -22,6 +22,8 @@ import java.nio.file.Paths
import com.google.gson.internal.LinkedTreeMap import com.google.gson.internal.LinkedTreeMap
import scala.util.Try
class RecMetaXmlHelper extends RecordingServiceGW with LogHelper { class RecMetaXmlHelper extends RecordingServiceGW with LogHelper {
val SUCCESS = "SUCCESS" val SUCCESS = "SUCCESS"
@ -206,7 +208,7 @@ class RecMetaXmlHelper extends RecordingServiceGW with LogHelper {
val mapTrack = it.next() val mapTrack = it.next()
list.add(new Track( list.add(new Track(
// TODO : change this later and provide authenticated/signed URLs to fetch the caption files // TODO : change this later and provide authenticated/signed URLs to fetch the caption files
href = captionBaseUrl + mapTrack.get("lang") + ".vtt", href = captionBaseUrl + mapTrack.get("kind") + "_" + mapTrack.get("lang") + ".vtt",
kind = mapTrack.get("kind"), kind = mapTrack.get("kind"),
label = mapTrack.get("label"), label = mapTrack.get("label"),
lang = mapTrack.get("lang"), lang = mapTrack.get("lang"),
@ -266,18 +268,25 @@ class RecMetaXmlHelper extends RecordingServiceGW with LogHelper {
} }
} }
def mv(oldName: String, newName: String) =
Try(new File(oldName).renameTo(new File(newName))).getOrElse(false)
def saveTrackInfoFile(trackInfoJson: String, trackInfoFilePath: String): Boolean = { def saveTrackInfoFile(trackInfoJson: String, trackInfoFilePath: String): Boolean = {
// Need to create intermediate file to prevent race where the file is processed before
// contents have been written.
val tempTrackInfoFilePath = trackInfoFilePath + ".tmp"
var result = false var result = false
val fileWriter = new FileWriter(trackInfoFilePath) val fileWriter = new FileWriter(tempTrackInfoFilePath)
try { try {
fileWriter.write(trackInfoJson) fileWriter.write(trackInfoJson)
result = true result = true
} catch { } catch {
case ioe: IOException => case ioe: IOException =>
logger.info("Failed to write caption.json {}", trackInfoFilePath) logger.info("Failed to write caption.json {}", tempTrackInfoFilePath)
result = false result = false
case ex: Exception => case ex: Exception =>
logger.info("Exception while writing {}", trackInfoFilePath) logger.info("Exception while writing {}", tempTrackInfoFilePath)
logger.info("Exception details: {}", ex.getMessage) logger.info("Exception details: {}", ex.getMessage)
result = false result = false
} finally { } finally {
@ -285,6 +294,11 @@ class RecMetaXmlHelper extends RecordingServiceGW with LogHelper {
fileWriter.close() fileWriter.close()
} }
if (result) {
// Rename so that the captions processor will pick up the uploaded captions.
result = mv(tempTrackInfoFilePath, trackInfoFilePath)
}
result result
} }