Ejemplo n.º 1
0
        static void Debug()
        {
            //AUP.UploadZip("44186", "47777", @"d:\temp\1.3.12.2.1107.5.1.4.65115.3000001603240736279390001264491.zip", "henryknipe", "20160411091821_henryknipe", "1.3.12.2.1107.5.8.9.13.26.65.26.126.218.77882191");
            APetaPoco.SetConnectionString("cn1");
            var bm    = APetaPoco.PpRetrieveOne <DbCase>("Cases", "[case_id] = '20160607180814_frank'");
            var pcase = (DbCase)bm.Data;

            bm = APetaPoco.PpRetrieveList <DbStudy>("Studies", "[case_id] = '20160607180814_frank'");
            pcase.study_list = (List <DbStudy>)bm.Data;
            foreach (var st in pcase.study_list)
            {
                bm        = APetaPoco.PpRetrieveList <DbSeries>("Series", "[case_id] = '20160607180814_frank' AND [study_uid] = '" + st.study_uid + "'");
                st.series = (List <DbSeries>)bm.Data;
                //ADCM.DownloadStudy(st);
                LOG.Write("Converting DCM to PNG...");
                AIMG.MultiFrameProcess(st);
                bool convertComplete = AIMG.ConvertDcmToPng(st);
                //if (!convertComplete)
                //{
                //    ACASE.ClearCaseFiles(pcase);
                //    throw new Exception("Unable to convert study to PNG");
                //}
                LOG.Write("Completed image conversion");

                LOG.Write("Optimizing PNG's for study...");
                AIMG.OptiPng(st);
                LOG.Write("Completed optimization.");
                //AIMG.ZipSeries(st);
            }
            //ACASE.ProcessCase(pcase);
        }
Ejemplo n.º 2
0
    public void HandleAllGetRequests(HttpListenerContext context)
    {
        var    rc        = new ResponseClass();
        string studyuid  = context.Request.QueryString["studyuid"];
        string seriesuid = context.Request.QueryString["seriesuid"];
        string check     = context.Request.QueryString["check"];

        if (!string.IsNullOrEmpty(check))
        {
            rc.Success = true;
            rc.Message = "";
            rc.Data    = true;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc);
            this.SendTextResponse(context, json);
            return;
        }
        var node = ADCM.GetSelectedNode();

        try
        {
            LOG.Write("New request");
            if (string.IsNullOrEmpty(studyuid) || string.IsNullOrEmpty(seriesuid))
            {
                throw new Exception("No studyuid or seriesuid provided");
            }
            bool downloaded = ADCM.DownloadOneSeries(studyuid, seriesuid);
            if (!downloaded)
            {
                throw new Exception("Unable to download study");
            }
            string seriesPath = Path.Combine(node.LocalStorage, studyuid, seriesuid);
            if (!Directory.Exists(seriesPath))
            {
                throw new Exception("Series path not found: " + seriesPath);
            }
            var     dcmFiles  = Directory.GetFiles(seriesPath, "*.dcm");
            string  filetouse = null;
            decimal mid       = dcmFiles.Length / 2;
            int     index     = (int)Math.Ceiling(mid);
            for (int i = index; i < dcmFiles.Length; i++)
            {
                var dcm = dcmFiles[i];
                ClearCanvas.Dicom.DicomFile dcmFile = new ClearCanvas.Dicom.DicomFile(dcm);
                dcmFile.Load();
                ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource localds = new ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource(dcmFile);
                if (!localds.IsImage)
                {
                    continue;
                }
                else
                {
                    filetouse = dcm;
                    break;
                }
            }
            if (string.IsNullOrEmpty(filetouse))
            {
                for (int i = 0; i < dcmFiles.Length; i++)
                {
                    var dcm = dcmFiles[i];
                    ClearCanvas.Dicom.DicomFile dcmFile = new ClearCanvas.Dicom.DicomFile(dcm);
                    dcmFile.Load();
                    ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource localds = new ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource(dcmFile);
                    if (!localds.IsImage)
                    {
                        continue;
                    }
                    else
                    {
                        filetouse = dcm;
                        break;
                    }
                }
            }
            if (string.IsNullOrEmpty(filetouse))
            {
                throw new Exception("Unable to find image in downloaded DICOM files");
            }
            if (!File.Exists(filetouse))
            {
                throw new Exception("Unable to find DICOM file to use");
            }

            string base64String = Convert.ToBase64String(AIMG.GetImageBytesFromDcm(filetouse));
            base64String = "data:image/jpeg;base64," + base64String;
            rc.Success   = true;
            rc.Message   = "";
            rc.Data      = base64String;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc);
            this.SendTextResponse(context, json);
        }
        catch (Exception ex)
        {
            LOG.Write("ERROR: " + ex.Message);
            rc.Data    = null;
            rc.Success = false;
            rc.Message = ex.Message;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc);
            this.SendTextResponse(context, json);
        }
        finally
        {
            string studypath = Path.Combine(node.LocalStorage, studyuid);
            if (Directory.Exists(studypath))
            {
                Directory.Delete(studypath, true);
            }
        }
    }
Ejemplo n.º 3
0
    public static void ProcessCase(DbCase pcase)
    {
        try
        {
            LOG.Write("Checking user quotas...");
            if (!CheckQuotas(pcase))
            {
                throw new Exception("Quota exceeded");
            }
            LOG.Write("Creating case on Radiopaedia...");
            pcase.r_case_id = AUP.CreateCase(pcase);
            if (string.IsNullOrEmpty(pcase.r_case_id))
            {
                throw new Exception("Unable to create case id, cannot continue");
            }
            LOG.Write("Case created id: " + pcase.r_case_id);

            if (pcase.study_list == null || pcase.study_list.Count < 1)
            {
                pcase.study_list = GetStudiesForCase(pcase.case_id);
                if (pcase.study_list == null || pcase.study_list.Count < 1)
                {
                    throw new Exception("No studies in case: " + pcase.case_id);
                }
            }

            foreach (var st in pcase.study_list)
            {
                LOG.InsertEvent("Starting to process case: " + pcase.case_id, "AGENT", null, pcase.case_id);
                LOG.Write("Creating Radiopaedia Study...");
                st.r_study_id = AUP.CreateStudy(st, pcase.username, pcase.r_case_id);
                if (string.IsNullOrEmpty(st.r_study_id))
                {
                    throw new Exception("Unable to create study id on Radiopaedia");
                }
                LOG.Write("Study ID created: " + st.r_study_id);

                LOG.Write("Study: " + st.description + "[" + st.modality + "]");
                LOG.Write("Downloading...");
                bool downloadComplete = ADCM.DownloadStudy(st, 0);
                if (!downloadComplete)
                {
                    ClearCaseFiles(pcase);
                    throw new Exception("Unable to download study - can't continue");
                }
                LOG.Write("Download finished");
                LOG.Write("Converting DCM to PNG...");
                AIMG.MultiFrameProcess(st);
                bool convertComplete = AIMG.ConvertDcmToPng(st);
                if (!convertComplete)
                {
                    ClearCaseFiles(pcase);
                    throw new Exception("Unable to convert study to PNG");
                }
                LOG.Write("Completed image conversion");

                LOG.Write("Deleting excess images...");
                AIMG.DeleteExcessImages(st);
                LOG.Write("Completed deleting excess images.");

                LOG.Write("Optimizing PNG's for study...");
                AIMG.OptiPng(st);
                LOG.Write("Completed optimization.");

                bool zipComplete = AIMG.ZipSeries(st);
                if (!zipComplete)
                {
                    throw new Exception("Unable to create zips for study");
                }


                string outPath = Path.Combine(ADCM.GetStoreString(), "OUTPUT", pcase.case_id, st.study_uid);
                var    zips    = Directory.GetFiles(outPath, "*.zip");
                foreach (var z in zips)
                {
                    string fileName = Path.GetFileName(z);

                    string[] sizes = { "B", "KB", "MB", "GB" };
                    double   len   = new FileInfo(z).Length;
                    int      order = 0;
                    while (len >= 1024 && ++order < sizes.Length)
                    {
                        len = len / 1024;
                    }

                    LOG.Write(string.Format("Uploading: {2} ({0:0.##} {1})", len, sizes[order], fileName));
                    bool uploadedZip = AUP.UploadZip2(pcase.r_case_id, st.r_study_id, z, pcase.username, pcase.case_id, st.study_uid);
                    if (!uploadedZip)
                    {
                        try
                        {
                            LOG.Write("Retry maxed out, copying zip to error output");
                            string errorFolder = Path.Combine(@".\Error_uploads\", pcase.case_id);
                            if (!Directory.Exists(errorFolder))
                            {
                                Directory.CreateDirectory(errorFolder);
                            }
                            string errorPath = Path.Combine(errorFolder, fileName);
                            File.Copy(z, errorPath);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                    LOG.Write("Finished uploading");
                }
            }
            LOG.Write("Marking case as completed");
            AUP.MarkCaseComplete(pcase.r_case_id, pcase.username, pcase.case_id);
            SetCaseStatus(pcase, "COMPLETED", "Case fully uploaded: http://radiopaedia.org/cases/" + pcase.r_case_id);
            System.Threading.Thread.Sleep(1000);
            LOG.Write("Finished with case: " + pcase.case_id);
            ClearCaseFiles(pcase);
            LOG.InsertEvent("Finished with case: " + pcase.case_id, "AGENT", null, pcase.case_id);
        }
        catch (Exception ex)
        {
            string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name;
            LOG.Write(errorString);
            LOG.Write(ex.Message);
            LOG.InsertEvent(errorString, "AGENT", ex.Message, pcase.case_id);
            SetCaseStatus(pcase, "ERROR", ex.Message);
            ClearCaseFiles(pcase);
        }
        finally { GC.Collect(); }
    }