private void readAndWriteToMediaJson() { while (writeQueue.Count > 0) { FileProperties properties = writeQueue.Dequeue(); var mediatype = properties.data.MediaType(); JObject parsedMedia = new JObject(); if (mediatype == MEDIATYPE.Image) { JObject image = templateImage; image["location"] = imageLocationRoot + properties.data.fileName; parsedMedia = image; } else if (mediatype == MEDIATYPE.Video) { JObject video = templateVideo; video["location"] = videoLocationRoot + properties.data.fileName; parsedMedia = video; } else if (mediatype == MEDIATYPE.Web) { //TODO: Add logic for web //Web video = new Web(); //Web = properties.data.filePath; } try { if (!File.Exists(properties.jsonFilePath)) { File.Copy(jsonFilePathMediaTemplate, properties.jsonFilePath); File.Copy(jsonFilePathPpShowlist, Path.Combine(new FileInfo(properties.jsonFilePath).Directory.FullName, jsonFileNamePpShowlist)); } var fileContent = File.ReadAllText(properties.jsonFilePath); var mediaInfo = JsonConvert.DeserializeObject <Media>(fileContent); mediaInfo.tracks.Add(parsedMedia); var mediaJsonString = JsonConvert.SerializeObject(mediaInfo); File.WriteAllText(properties.jsonFilePath, mediaJsonString); if (properties.data.approved) { string pendingjsonPath = properties.jsonFilePath.Replace("Approved", "Pending"); var pendingFileContent = File.ReadAllText(pendingjsonPath); var pendingMediaInfo = JsonConvert.DeserializeObject <Media>(pendingFileContent); pendingMediaInfo.tracks.RemoveAll(p => p.ToString().Contains(properties.data.fileName)); var updatedApprovalmediaJson = JsonConvert.SerializeObject(pendingMediaInfo); File.WriteAllText(pendingjsonPath, updatedApprovalmediaJson); } } catch { } } }
public void writeToMediaJson(string jsonFilePath, FileData fileData, bool isDeleteOperation) { lock (writeQueue) { // Create the entry and push to the Queue FileProperties fileProperties = new FileProperties(jsonFilePath, fileData, isDeleteOperation); writeQueue.Enqueue(fileProperties); // If we have reached the Queue Size then flush the Queue if (writeQueue.Count >= queueSize || DoPeriodicFlush()) { readAndWriteToMediaJson(); } } }