private void WriteStudyStream(string streamFile, string gzStreamFile, StudyXml theStream) { XmlDocument doc = theStream.GetMemento(_outputSettings); // allocate the random number generator here, in case we need it below var rand = new Random(); string tmpStreamFile = streamFile + "_tmp"; string tmpGzStreamFile = gzStreamFile + "_tmp"; for (int i = 0; ; i++) { try { if (File.Exists(tmpStreamFile)) { FileUtils.Delete(tmpStreamFile); } if (File.Exists(tmpGzStreamFile)) { FileUtils.Delete(tmpGzStreamFile); } _fileSaved = true; using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(tmpStreamFile, FileMode.CreateNew), gzipStream = FileStreamOpener.OpenForSoleUpdate(tmpGzStreamFile, FileMode.CreateNew)) { StudyXmlIo.WriteXmlAndGzip(doc, xmlStream, gzipStream); xmlStream.Close(); gzipStream.Close(); } if (File.Exists(streamFile)) { FileUtils.Delete(streamFile); } File.Move(tmpStreamFile, streamFile); if (File.Exists(_gzPath)) { FileUtils.Delete(_gzPath); } File.Move(tmpGzStreamFile, _gzPath); return; } catch (IOException) { if (i < 5) { Thread.Sleep(rand.Next(5, 50)); // Sleep 5-50 milliseconds continue; } throw; } } }
private void SaveStudyXml(StudyXml studyXml) { XmlDocument doc = studyXml.GetMemento(new StudyXmlOutputSettings()); using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(StorageLocation.GetStudyXmlPath(), FileMode.Create), gzipStream = FileStreamOpener.OpenForSoleUpdate(StorageLocation.GetCompressedStudyXmlPath(), FileMode.Create)) { StudyXmlIo.WriteXmlAndGzip(doc, xmlStream, gzipStream); xmlStream.Close(); gzipStream.Close(); } }
protected override void OnExecute(CommandProcessor theProcessor) { // Make sure the directory exists where we're storing the file. var p = Path.GetDirectoryName(_path); if (string.IsNullOrEmpty(p) || !Directory.Exists(p)) { if (!theProcessor.ExecuteSubCommand(this, new CreateDirectoryCommand(Path.GetDirectoryName(_path)))) { throw new ApplicationException(theProcessor.FailureReason); } } if (RequiresRollback) { Backup(); } string path = GetTempPath(); using (FileStream stream = FileStreamOpener.OpenForSoleUpdate(path, _failOnExists ? FileMode.CreateNew : FileMode.Create)) { _file.Save(stream, DicomWriteOptions.Default); stream.Flush(); stream.Close(); } if (_failOnExists && File.Exists(_path)) { // Do this test after creating the temp folder in case another thread is receiving the file at the same // time. try { // Delete the temp file we saved FileUtils.Delete(path); } catch (Exception x) { throw new ApplicationException(String.Format("DICOM File unexpectedly already exists: {0}", _path), x); } throw new ApplicationException(String.Format("DICOM File unexpectedly already exists: {0}", _path)); } FileUtils.Copy(path, _path, true); _fileCreated = true; FileUtils.Delete(path); }
/// <summary> /// Save the <see cref="StudyXml"/> file for a study. /// </summary> /// <param name="studyXml">The <see cref="StudyXml"/> file to save.</param> /// <param name="fileCreated">flag set to true if the file was created</param> public void SaveStudyXml(StudyXml studyXml, out bool fileCreated) { var settings = new StudyXmlOutputSettings { IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag, IncludeUnknownTags = StudyXmlTagInclusion.IgnoreTag, IncludeLargeTags = StudyXmlTagInclusion.IncludeTagExclusion, MaxTagLength = 2048, IncludeSourceFileName = true }; var doc = studyXml.GetMemento(settings); string streamFile = GetStudyXmlPath(); // allocate the random number generator here, in case we need it below var rand = new Random(); string tmpStreamFile = streamFile + "_tmp"; for (int i = 0; ; i++) { try { if (File.Exists(tmpStreamFile)) { FileUtils.Delete(tmpStreamFile); } using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(tmpStreamFile, FileMode.CreateNew)) { StudyXmlIo.Write(doc, xmlStream); xmlStream.Close(); } File.Copy(tmpStreamFile, streamFile, true); fileCreated = true; FileUtils.Delete(tmpStreamFile); return; } catch (IOException) { if (i < 5) { Thread.Sleep(rand.Next(5, 50)); // Sleep 5-50 milliseconds continue; } throw; } } }
private void UpdateFilesystem() { Platform.Log(LogLevel.Info, "Updating filesystem..."); StudyXml studyXml = _oldStudyLocation.LoadStudyXml(); StudyXmlOutputSettings outputSettings = ImageServerCommonConfiguration.DefaultStudyXmlOutputSettings; StudyXml newStudyXml = new StudyXml(); foreach (SeriesXml seriesXml in studyXml) { foreach (InstanceXml instanceXml in seriesXml) { string path = Path.Combine(_oldStudyPath, seriesXml.SeriesInstanceUid); path = Path.Combine(path, instanceXml.SopInstanceUid); path += ServerPlatform.DicomFileExtension; if (!File.Exists(path)) { Platform.Log(LogLevel.Info, "SOP {0} is referenced in study xml but does not exist. It will be removed"); continue; // file was removed but xml was not updated? } try { DicomFile file = new DicomFile(path); file.Load(); InstanceInfo instance = new InstanceInfo { SeriesInstanceUid = file.DataSet[DicomTags.SeriesInstanceUid].GetString(0, String.Empty), SopInstanceUid = file.DataSet[DicomTags.SopInstanceUid].GetString(0, String.Empty) }; UpdateDicomFile(file); // Add into the temporary study xml long fileSize = 0; if (File.Exists(file.Filename)) { FileInfo finfo = new FileInfo(file.Filename); fileSize = finfo.Length; } newStudyXml.AddFile(file, fileSize, outputSettings); _updatedSopList.Add(instance); Platform.Log(ServerPlatform.InstanceLogLevel, "SOP {0} has been updated [{1} of {2}].", instance.SopInstanceUid, _updatedSopList.Count, _totalSopCount); } catch (Exception) { File.Delete(Path.Combine(_backupDir, instanceXml.SopInstanceUid) + ".bak"); //dont' need to restore this file throw; } } } // Log any study-level warnings if (_updatedSopList.Count != _totalSopCount) { Platform.Log(LogLevel.Warn, "Inconsistent data: expected {0} instances to be updated / Found {1}.", _totalSopCount, _updatedSopList.Count); } // update the header Platform.Log(LogLevel.Info, "Generating new study header..."); string newStudyXmlPath = Path.Combine(NewStudyPath, _newStudyInstanceUid + ".xml"); string gzipStudyXmlPath = Path.Combine(NewStudyPath, _newStudyInstanceUid + ".xml.gz"); using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(newStudyXmlPath, FileMode.Create), gzipStream = FileStreamOpener.OpenForSoleUpdate(gzipStudyXmlPath, FileMode.Create)) { StudyXmlIo.WriteXmlAndGzip(newStudyXml.GetMemento(outputSettings), xmlStream, gzipStream); xmlStream.Close(); gzipStream.Close(); } }
protected override void OnExecute(CommandProcessor theProcessor) { if (_path == null) { String seriesUid = _file.DataSet[DicomTags.SeriesInstanceUid].GetString(0, String.Empty); String sopUid = _file.DataSet[DicomTags.SopInstanceUid].GetString(0, String.Empty); _path = _storageLocation.GetSopInstancePath(seriesUid, sopUid); } // Make sure the directory exists where we're storing the file. var p = Path.GetDirectoryName(_path); if (string.IsNullOrEmpty(p) || !Directory.Exists(p)) { if (!theProcessor.ExecuteSubCommand(this, new CreateDirectoryCommand(Path.GetDirectoryName(_path)))) { throw new ApplicationException(theProcessor.FailureReason); } } if (RequiresRollback) { Backup(); } string path = _saveTemp ? GetTempPath() : _path; using (FileStream stream = FileStreamOpener.OpenForSoleUpdate(path, FileMode.Create)) { // Set _fileCreated here, because the file has been opened. if (!_saveTemp) { _fileCreated = true; } _saveSpeed.Start(); _file.Save(stream, DicomWriteOptions.Default); stream.Flush(); stream.Close(); _saveSpeed.End(); var fi = new FileInfo(path); _saveSpeed.SetData(fi.Length); } if (_saveTemp) { if (File.Exists(_path)) { if (_failOnExists) { try { FileUtils.Delete(path); } catch (Exception x) { throw new ApplicationException(String.Format("DICOM File unexpectedly already exists: {0}", _path), x); } throw new ApplicationException(String.Format("DICOM File unexpectedly already exists: {0}", _path)); } FileUtils.Delete(_path); } File.Move(path, _path); _fileCreated = true; } }