Exemplo n.º 1
0
        private async Task <AnalysedImage> ResolveImageAsync(QueuedImage queuedImage)
        {
            ImageAnalysis result;

            do
            {
                await Task.Delay(1000); /* delay one before querying as results aren't processed super fast */

                var response = await _httpClient.GetAsync(queuedImage.ResourceURL);

                result = JsonConvert.DeserializeObject <ImageAnalysis>(await response.Content.ReadAsStringAsync());
            } while (!result.IsSuccessStatus);


            return(new AnalysedImage()
            {
                Result = result,
                QueuedImage = queuedImage
            });
        }
Exemplo n.º 2
0
        public DicomDataset GetNextDatasetToProcess(out string filename, out Dictionary <string, string> otherValuesToStoreInRow)
        {
            otherValuesToStoreInRow = new Dictionary <string, string>();

            if (_progress >= _messages.Count)
            {
                filename = null;
                return(null);
            }

            QueuedImage toReturn = _messages[_progress];

            filename = toReturn.DicomFileMessage.DicomFilePath;

            otherValuesToStoreInRow.Add("MessageGuid", _messages[_progress].Header.MessageGuid.ToString());
            otherValuesToStoreInRow.Add("DicomFileSize", toReturn.DicomFileMessage.DicomFileSize.ToString()); //TN: It won't be a string when it hits the database but the API supports only string/string for this out Dictionary

            _progress++;

            return(toReturn.DicomDataset);
        }
        // ReSharper disable once MemberCanBeMadeStatic.Local
        private async Task <bool> HandleQueuedImage(QueuedImage queuedImage)
        {
            if (queuedImage.Status != 1)
            {
                // Ignoring this entry in queue. If it's not completed, it will be handled by the cleanup worker.
                return(true);
            }

            var(orgFile, orgFileContentType) = await Utils.GetFileStreamAndContentType(_pdb,
                                                                                       queuedImage.UserId,
                                                                                       queuedImage.Path);

            if (!Utils.ContentTypeIsImage(orgFileContentType))
            {
                // TODO: Implement this
                return(true);
            }

            var fileHash = Utils.GetHashString(orgFile);

            var createImageFilesResult = _imageHelper.CreateImageFiles(orgFile, orgFileContentType, fileHash);

            createImageFilesResult.AlbumImageMap.AlbumId = queuedImage.AlbumId;

            var insertImageSuccess = await _pdb.AddImageToAlbum(
                createImageFilesResult.Image,
                createImageFilesResult.AlbumImageMap,
                createImageFilesResult.Sizes,
                createImageFilesResult.Files,
                queuedImage.QueuedImageId);

            Console.WriteLine($"[Handle queuedImage (id: {queuedImage.QueuedImageId}, " +
                              $"success: {insertImageSuccess.ToString()})] userId: {queuedImage.UserId}, " +
                              $"albumId: {queuedImage.AlbumId}, " +
                              $"virtualPath: {Utils.Base64Decode(queuedImage.Path)}, realPath: {orgFile.Name}, " +
                              $"contentType: {orgFileContentType}, fileHash: {fileHash}");

            return(true);
        }
Exemplo n.º 4
0
        public static void ProcessImages(List <string> readErrors, DeltaExportPatch patch)
        {
            //Clean up any old and bad paths
            Console.WriteLine("Cleaning up old image conversions...");
            if (Directory.Exists("./Lib/UModel/in_temp/"))
            {
                Directory.Delete("./Lib/UModel/in_temp/", true);
            }
            if (Directory.Exists("./Lib/UModel/out_temp/"))
            {
                Directory.Delete("./Lib/UModel/out_temp/", true);
            }

            //Make structre
            Directory.CreateDirectory("./Lib/UModel/in_temp/");
            Directory.CreateDirectory("./Lib/UModel/out_temp/");

            //Get the queue
            var queue = patch.queued_images;

            //First, we copy all packages to a temporary path with their index
            Console.WriteLine($"Now copying {queue.Count} images...");
            for (int i = 0; i < queue.Count; i++)
            {
                string source = queue[i].pathname;
                File.Copy(source, $"./Lib/UModel/in_temp/{i}.uasset");
            }

            //Now, run the conversion
            Console.WriteLine("Now converting images using UModel...");
            Process p = Process.Start(new ProcessStartInfo
            {
                Arguments        = "",
                FileName         = "go.bat",
                WorkingDirectory = "Lib\\UModel\\",
                UseShellExecute  = true
            });

            p.WaitForExit();

            //Now, load and process these images
            int ok = 0;

            Console.WriteLine($"Now processing {queue.Count} images...");
            for (int i = 0; i < queue.Count; i += 1)
            {
                QueuedImage q      = queue[i];
                bool        status = false;

                try
                {
                    //Get the directory. It's a little janky, as files are stored in subdirs
                    string[] results = Directory.GetFiles($"./Lib/UModel/out_temp/{i}/");
                    if (results.Length != 1)
                    {
                        throw new Exception("None or too many results found for image.");
                    }

                    //Open FileStream on this
                    using (FileStream imgStream = new FileStream(results[0], FileMode.Open, FileAccess.Read))
                    {
                        //Now, begin reading the TGA data https://en.wikipedia.org/wiki/Truevision_TGA
                        IOMemoryStream imgReader = new IOMemoryStream(imgStream, true);
                        imgReader.position += 3 + 5; //Skip intro, it will always be known
                        imgReader.ReadShort();       //Will always be 0
                        imgReader.ReadShort();       //Will aways be 0
                        short width      = imgReader.ReadShort();
                        short height     = imgReader.ReadShort();
                        byte  colorDepth = imgReader.ReadByte();
                        imgReader.ReadByte();

                        //Now, we can begin reading image data
                        //This appears to be bugged for non-square images right now.
                        using (Image <Rgba32> img = new Image <Rgba32>(width, height))
                        {
                            //Read file
                            byte[] channels;
                            for (int y = 0; y < height; y++)
                            {
                                for (int x = 0; x < width; x++)
                                {
                                    if (colorDepth == 32)
                                    {
                                        //Read four channels
                                        channels = imgReader.ReadBytes(4);

                                        //Set pixel
                                        img[x, width - y - 1] = new Rgba32(channels[2], channels[1], channels[0], channels[3]);
                                    }
                                    else if (colorDepth == 24)
                                    {
                                        //Read three channels
                                        channels = imgReader.ReadBytes(3);

                                        //Set pixel
                                        img[x, width - y - 1] = new Rgba32(channels[2], channels[1], channels[0]);
                                    }
                                }
                            }

                            //Apply mods
                            if (q.mods == ImageModifications.White)
                            {
                                ApplyWhiteMod(img);
                            }

                            //Save original image
                            using (MemoryStream ms = new MemoryStream())
                            {
                                img.SaveAsPng(ms);
                                ms.Position = 0;
                                patch.asset_manager.Upload(Program.config.GetProfile().upload_images + q.hiId + FORMAT_TYPE, ms);
                            }

                            //Now, downscale
                            img.Mutate(x => x.Resize(64, 64));

                            //Save thumbnail
                            using (MemoryStream ms = new MemoryStream())
                            {
                                img.SaveAsPng(ms, new PngEncoder
                                {
                                    CompressionLevel = 9
                                });
                                ms.Position = 0;
                                patch.asset_manager.Upload(Program.config.GetProfile().upload_images + q.loId + FORMAT_TYPE, ms);
                            }

                            status = true;
                            ok++;
                        }
                    }
                } catch (Exception ex)
                {
                    Console.WriteLine($"Failed to process image {q.classname} with error {ex.Message}");
                    readErrors.Add($"Failed to process image {q.classname} with error {ex.Message} {ex.StackTrace}");
                }

                //Now, add to persistent storage
                patch.persist.external_assets.Add(new DeltaExportBranchExternalAsset
                {
                    name     = q.name,
                    patch    = patch.tag,
                    sha1     = q.sha1,
                    time     = DateTime.UtcNow,
                    id_hires = q.hiId,
                    id_lores = q.loId,
                    ok       = status
                });
            }
            Log.WriteSuccess("ImageTool", $"Processed and uploading {ok}/{queue.Count} images.");
            queue.Clear();

            //Clean up any old and bad paths
            Console.WriteLine("Cleaning up...");
            if (Directory.Exists("./Lib/UModel/in_temp/"))
            {
                Directory.Delete("./Lib/UModel/in_temp/", true);
            }
            if (Directory.Exists("./Lib/UModel/out_temp/"))
            {
                Directory.Delete("./Lib/UModel/out_temp/", true);
            }
        }
Exemplo n.º 5
0
 public UploadImageResult(QueuedImage img, Basic<UploadData> result)
 {
     Image = img;
     Result = result;
 }