Пример #1
0
        protected override void OnExecute(CommandProcessor theProcessor)
        {
            var rand = new Random();

            if (Diagnostics.Settings.SimulateFileCorruption)
            {
                RandomError.Generate(
                    rand.Next() % 2 == 0,
                    String.Format("Corrupting the file {0}", _path),
                    delegate
                {
                    var f     = new FileInfo(_path);
                    long size = rand.Next(0, (int)f.Length / 2);
                    if (size <= 0)
                    {
                        FileStream s = FileStreamOpener.OpenForSoleUpdate(_path, FileMode.Truncate);
                        s.Flush();
                        s.Close();
                    }
                    else
                    {
                        FileStream s  = FileStreamOpener.OpenForRead(_path, FileMode.Open);
                        var buffer    = new byte[size];
                        int bytesRead = s.Read(buffer, 0, buffer.Length);
                        s.Close();

                        s = FileStreamOpener.OpenForSoleUpdate(_path, FileMode.Truncate);
                        s.Write(buffer, 0, bytesRead);
                        s.Flush();
                        s.Close();
                    }
                }
                    );
            }
        }
Пример #2
0
        public bool SaveStreamData(DicomMessage message, byte[] data, int offset, int length)
        {
            if (_rejectFile)
            {
                return(true);
            }

            if (_fileStream == null)
            {
                _sourceFolder = _context.StorageConfiguration.FileStoreIncomingFolder;

                _sourceFilename = Path.Combine(_sourceFolder, Guid.NewGuid().ToString() + ".cc");

                try
                {
                    _fileStream = FileStreamOpener.OpenForSoleUpdate(_sourceFilename, FileMode.Create);
                }
                catch (Exception x)
                {
                    Platform.Log(LogLevel.Warn, x, "Unable to open file for saving filestream: {0}", _sourceFilename);
                    return(false);
                }
            }

            _fileStream.Write(data, offset, length);

            return(true);
        }
Пример #3
0
        public static void Write(StudyXmlMemento theMemento, string filename)
        {
            if (theMemento.RootNode != null)
            {
                using (var fs = FileStreamOpener.OpenForSoleUpdate(filename, FileMode.CreateNew))
                {
                    Write(theMemento, fs);
                }
            }
            else
            {
                var xmlSettings = new XmlWriterSettings
                {
                    Encoding            = Encoding.UTF8,
                    ConformanceLevel    = ConformanceLevel.Document,
                    Indent              = false,
                    NewLineOnAttributes = false,
                    CheckCharacters     = true,
                    IndentChars         = string.Empty
                };

                XmlWriter tw = XmlWriter.Create(filename, xmlSettings);
                theMemento.Document.WriteTo(tw);
                tw.Flush();
                tw.Close();
            }
        }
Пример #4
0
        public bool SaveStreamData(DicomMessage message, byte[] data, int offset, int count)
        {
            ISopInstanceImporter importer = IoC.Get <ISopInstanceImporter>();

            importer.Context = _importContext;

            var sopInstanceUid = message.DataSet[DicomTags.SopInstanceUid].GetString(0, string.Empty);

            if (_fileStream == null)
            {
                if (!importer.GetStreamedFileStorageFolder(message, out _sourceFolder, out _filesystemStreamingFolder))
                {
                    Platform.Log(LogLevel.Warn, "Unable to create a folder to save SOP Instance, rejecting: {0}", sopInstanceUid);
                    return(false);
                }

                _sourceFilename = Path.Combine(_sourceFolder, Guid.NewGuid().ToString() + "dcm");

                try
                {
                    _fileStream = FileStreamOpener.OpenForSoleUpdate(_sourceFilename, FileMode.Create);
                }
                catch (Exception x)
                {
                    Platform.Log(LogLevel.Warn, x, "Unable to open file for saving filestream: {0}", _sourceFilename);
                    return(false);
                }
            }

            _fileStream.Write(data, offset, count);

            return(true);
        }
Пример #5
0
        private void SendFilePresentationContext(DicomClient client, byte pcid, StorageInstance fileToSend)
        {
            fileToSend.SentMessageId = client.NextMessageID();

            if (fileToSend.MetaInfoFileLength == 0)
            {
                DicomFile theFile = new DicomFile(fileToSend.Filename);
                theFile.Load(DicomTags.RelatedGeneralSopClassUid, DicomReadOptions.Default);
                fileToSend.MetaInfoFileLength = theFile.MetaInfoFileLength;
            }

            using (var fs = FileStreamOpener.OpenForRead(fileToSend.Filename, FileMode.Open))
            {
                // Seek to the Dataset
                fs.Seek(fileToSend.MetaInfoFileLength, SeekOrigin.Begin);

                if (_moveOriginatorAe == null)
                {
                    client.SendCStoreRequest(pcid, fileToSend.SentMessageId, DicomPriority.Medium, null, 0, fileToSend.SopInstanceUid,
                                             fileToSend.SopClass.Uid, fs);
                }
                else
                {
                    client.SendCStoreRequest(pcid, fileToSend.SentMessageId, DicomPriority.Medium, _moveOriginatorAe, _moveOriginatorMessageId, fileToSend.SopInstanceUid,
                                             fileToSend.SopClass.Uid, fs);
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Get a list of paths to the first image in each series within the study being processed.
        /// </summary>
        /// <returns></returns>
        private List <string> GetFirstInstanceInEachStudySeries()
        {
            var fileList = new List <string>();

            if (_studyXml == null)
            {
                string studyXml = _location.GetStudyXmlPath();

                if (!File.Exists(studyXml))
                {
                    return(fileList);
                }

                _studyXml = new StudyXml();

                using (FileStream stream = FileStreamOpener.OpenForRead(studyXml, FileMode.Open))
                {
                    var theDoc = new XmlDocument();
                    StudyXmlIo.Read(theDoc, stream);
                    stream.Close();
                    _studyXml.SetMemento(theDoc);
                }
            }

            // Note, we try and force ourselves to have an uncompressed
            // image, if one exists.  That way the rules will be reapplied on the object
            // if necessary for compression.
            foreach (SeriesXml seriesXml in _studyXml)
            {
                InstanceXml saveInstance = null;

                foreach (InstanceXml instance in seriesXml)
                {
                    if (instance.TransferSyntax.Encapsulated)
                    {
                        if (saveInstance == null)
                        {
                            saveInstance = instance;
                        }
                    }
                    else
                    {
                        saveInstance = instance;
                        break;
                    }
                }

                if (saveInstance != null)
                {
                    string path = Path.Combine(_location.GetStudyPath(), seriesXml.SeriesInstanceUid);
                    path = Path.Combine(path, saveInstance.SopInstanceUid + ServerPlatform.DicomFileExtension);
                    fileList.Add(path);
                }
            }

            return(fileList);
        }
Пример #7
0
        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();
            }
        }
Пример #9
0
        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);
        }
        private static StudyXml GetStudyXml(StudyStorageLocation storageLocation)
        {
            StudyXml studyXml     = new StudyXml();
            string   studyXmlPath = Path.Combine(storageLocation.GetStudyPath(), storageLocation.StudyInstanceUid + ".xml");

            using (Stream stream = FileStreamOpener.OpenForRead(studyXmlPath, FileMode.Open))
            {
                var theMemento = new StudyXmlMemento();
                StudyXmlIo.Read(theMemento, stream);
                studyXml.SetMemento(theMemento);
                stream.Close();
            }
            return(studyXml);
        }
Пример #11
0
        /// <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;
                }
            }
        }
Пример #12
0
        /// <summary>
        /// Load the StudyXml file.
        /// </summary>
        /// <param name="studyXmlFile"></param>
        public void LoadStudyXml(string studyXmlFile)
        {
            using (Stream fileStream = FileStreamOpener.OpenForRead(studyXmlFile, FileMode.Open))
            {
                var theMemento = new StudyXmlMemento();

                StudyXmlIo.Read(theMemento, fileStream);

                _studyXml = new StudyXml(_storageLocation.StudyInstanceUid);
                _studyXml.SetMemento(theMemento);

                fileStream.Close();
            }
        }
Пример #13
0
        /// <summary>
        /// Helper method to load a <see cref="StudyXml"/> instance for a given study location.
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public static StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            StudyXml theXml = new StudyXml();

            if (File.Exists(streamFile))
            {
                // allocate the random number generator here, in case we need it below
                Random rand = new Random();

                // Go into a retry loop, to handle if the study is being processed right now
                for (int i = 0; ; i++)
                {
                    try
                    {
                        using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                        {
                            XmlDocument theDoc = new XmlDocument();

                            StudyXmlIo.Read(theDoc, fileStream);

                            theXml.SetMemento(theDoc);

                            fileStream.Close();

                            return(theXml);
                        }
                    }
                    catch (IOException)
                    {
                        if (i < 5)
                        {
                            Thread.Sleep(rand.Next(5, 50)); // Sleep 5-50 milliseconds
                            continue;
                        }

                        throw;
                    }
                }
            }

            return(theXml);
        }
Пример #14
0
        /// <summary>
        /// Loads the compressed header stream for the study with the specified study instance uid
        /// </summary>
        /// <returns>
        /// The compressed study header stream or null if the study doesn't exist.
        /// </returns>
        /// <remarks>
        /// </remarks>
        public Stream Load()
        {
            if (!StudyExists)
            {
                return(null);
            }

            _statistics.LoadHeaderStream.Start();
            String studyPath = StudyLocation.GetStudyPath();

            if (!Directory.Exists(studyPath))
            {
                // the study exist in the database but not on the filesystem.

                // TODO: If the study is migrated to another tier and the study folder is removed,
                // we may want to do something here instead of throwing exception.
                _statistics.LoadHeaderStream.End();
                throw new ApplicationException(String.Format("Study Folder {0} doesn't exist", studyPath));
            }

            String compressedHeaderFile = Path.Combine(studyPath, _studyInstanceUid + ".xml.gz");
            Stream headerStream         = null;

            Platform.Log(LogLevel.Debug, "Study Header Path={0}", compressedHeaderFile);
            try
            {
                headerStream = FileStreamOpener.OpenForRead(compressedHeaderFile, FileMode.Open, 30000 /* try for 30 seconds */);
            }
            catch (FileNotFoundException)
            {
                throw;
            }
            catch (IOException ex)
            {
                // treated as sharing violation
                throw new StudyAccessException("Study header is not accessible at this time.", StudyLocation.QueueStudyStateEnum, ex);
            }
            _statistics.LoadHeaderStream.End();
            _statistics.Size = (ulong)headerStream.Length;
            return(headerStream);
        }
Пример #15
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for the <see cref="StudyLocation"/>
        /// </summary>
        /// <returns>The <see cref="StudyXml"/> instance</returns>
        public StudyXml LoadStudyXml()
        {
            var theXml = new StudyXml();

            string streamFile = GetStudyXmlPath();

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }
            return(theXml);
        }
Пример #16
0
		/// <summary>
		/// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
		/// </summary>
		/// <returns>The <see cref="StudyXml"/> instance for the study</returns>
		private StudyXml LoadStudyXml()
		{
			StudyXml theXml = new StudyXml();

			String streamFile = Path.Combine(_rootPath, _studyInstanceUid + ".xml");
			if (File.Exists(streamFile))
			{
				using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
				{
					XmlDocument theDoc = new XmlDocument();

					StudyXmlIo.Read(theDoc, fileStream);

					theXml.SetMemento(theDoc);

					fileStream.Close();
				}
			}

			return theXml;
		}
Пример #17
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
        /// </summary>
        /// <param name="location">The location a study is stored.</param>
        /// <returns>The <see cref="StudyXml"/> instance for <paramref name="location"/></returns>
        protected virtual StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            StudyXml theXml = new StudyXml();

            String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    XmlDocument theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }

            return(theXml);
        }
Пример #18
0
        /// <summary>
        /// Load a <see cref="StudyXml"/> file for a given <see cref="StudyStorageLocation"/>
        /// </summary>
        /// <returns>The <see cref="StudyXml"/> instance for the study</returns>
        private StudyXml LoadStudyXml()
        {
            var theXml = new StudyXml();

            String streamFile = Path.Combine(_rootPath, _studyInstanceUid + ".xml");

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theMemento = new StudyXmlMemento();

                    StudyXmlIo.Read(theMemento, fileStream);

                    theXml.SetMemento(theMemento);

                    fileStream.Close();
                }
            }

            return(theXml);
        }
Пример #19
0
        /// <summary>
        /// Load the first instance from the first series of the StudyXml file for a study.
        /// </summary>
        /// <param name="location">The storage location of the study.</param>
        /// <returns></returns>
        protected static DicomFile LoadInstance(StudyStorageLocation location)
        {
            string studyXml = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            if (!File.Exists(studyXml))
            {
                return(null);
            }

            FileStream stream     = FileStreamOpener.OpenForRead(studyXml, FileMode.Open);
            var        theMemento = new StudyXmlMemento();

            StudyXmlIo.Read(theMemento, stream);
            stream.Close();
            stream.Dispose();
            var xml = new StudyXml();

            xml.SetMemento(theMemento);

            IEnumerator <SeriesXml> seriesEnumerator = xml.GetEnumerator();

            if (seriesEnumerator.MoveNext())
            {
                SeriesXml seriesXml = seriesEnumerator.Current;
                IEnumerator <InstanceXml> instanceEnumerator = seriesXml.GetEnumerator();
                if (instanceEnumerator.MoveNext())
                {
                    InstanceXml instance = instanceEnumerator.Current;
                    var         file     = new DicomFile("file.dcm", new DicomAttributeCollection(), instance.Collection)
                    {
                        TransferSyntax = instance.TransferSyntax
                    };
                    return(file);
                }
            }

            return(null);
        }
Пример #20
0
        protected static StudyXml LoadStudyXml(StudyStorageLocation location)
        {
            // This method should be combined with StudyStorageLocation.LoadStudyXml()
            StudyXml theXml = new StudyXml(location.StudyInstanceUid);

            String streamFile = Path.Combine(location.GetStudyPath(), location.StudyInstanceUid + ".xml");

            if (File.Exists(streamFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(streamFile, FileMode.Open))
                {
                    var theMemento = new StudyXmlMemento();

                    StudyXmlIo.Read(theMemento, fileStream);

                    theXml.SetMemento(theMemento);

                    fileStream.Close();
                }
            }

            return(theXml);
        }
        public MimeTypeProcessorOutput Process(ImageStreamingContext context)
        {
            MimeTypeProcessorOutput output = new MimeTypeProcessorOutput();

            output.ContentType = OutputMimeType;
            using (FileStream stream = FileStreamOpener.OpenForRead(context.ImagePath, FileMode.Open))
            {
                output.ContentType = OutputMimeType;
                byte[] buffer    = new byte[stream.Length];
                int    offset    = 0;
                int    readBytes = 0;
                do
                {
                    readBytes = stream.Read(buffer, offset, buffer.Length - offset);
                    if (readBytes > 0)
                    {
                        offset += readBytes;
                    }
                } while (readBytes > 0);
                output.Output = buffer;
                stream.Close();
            }
            return(output);
        }
Пример #22
0
        /// <summary>
        /// Apply the rules.
        /// </summary>
        /// <remarks>
        /// When rules are applied, we are simply adding new <see cref="ServerDatabaseCommand"/> instances
        /// for the rules to the currently executing <see cref="ServerCommandProcessor"/>.  They will be
        /// executed after all other rules have been executed.
        /// </remarks>
        protected override void OnExecute(CommandProcessor theProcessor)
        {
            string   studyXmlFile = Path.Combine(_directory, String.Format("{0}.xml", _studyInstanceUid));
            StudyXml theXml       = new StudyXml(_studyInstanceUid);

            if (File.Exists(studyXmlFile))
            {
                using (Stream fileStream = FileStreamOpener.OpenForRead(studyXmlFile, FileMode.Open))
                {
                    XmlDocument theDoc = new XmlDocument();

                    StudyXmlIo.Read(theDoc, fileStream);

                    theXml.SetMemento(theDoc);

                    fileStream.Close();
                }
            }
            else
            {
                string errorMsg = String.Format("Unable to load study XML file of restored study: {0}", studyXmlFile);

                Platform.Log(LogLevel.Error, errorMsg);
                throw new ApplicationException(errorMsg);
            }

            DicomFile defaultFile   = null;
            bool      rulesExecuted = false;

            foreach (SeriesXml seriesXml in theXml)
            {
                foreach (InstanceXml instanceXml in seriesXml)
                {
                    // Skip non-image objects
                    if (instanceXml.SopClass.Equals(SopClass.KeyObjectSelectionDocumentStorage) ||
                        instanceXml.SopClass.Equals(SopClass.GrayscaleSoftcopyPresentationStateStorageSopClass) ||
                        instanceXml.SopClass.Equals(SopClass.BlendingSoftcopyPresentationStateStorageSopClass) ||
                        instanceXml.SopClass.Equals(SopClass.ColorSoftcopyPresentationStateStorageSopClass))
                    {
                        // Save the first one encountered, just in case the whole study is non-image objects.
                        if (defaultFile == null)
                        {
                            defaultFile = new DicomFile("test", new DicomAttributeCollection(), instanceXml.Collection);
                        }
                        continue;
                    }

                    DicomFile file = new DicomFile("test", new DicomAttributeCollection(), instanceXml.Collection);
                    _context.Message = file;
                    _engine.Execute(_context);
                    rulesExecuted = true;
                    break;
                }
                if (rulesExecuted)
                {
                    break;
                }
            }

            if (!rulesExecuted && defaultFile != null)
            {
                _context.Message = defaultFile;
                _engine.Execute(_context);
            }
        }
Пример #23
0
        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();
            }
        }
Пример #24
0
        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;
            }
        }