public string Process(FileUpload input)
        {
            if (input?.Urls?.Count > 0)
            {
                try
                {
                    string picPath     = _environment.WebRootPath + "\\Upload\\";
                    string previewPath = _environment.WebRootPath + "\\Preview\\";
                    if (!Directory.Exists(picPath))
                    {
                        Directory.CreateDirectory(picPath);
                    }
                    if (!Directory.Exists(previewPath))
                    {
                        Directory.CreateDirectory(previewPath);
                    }

                    string result         = "";
                    int    successCounter = 0;
                    int    curCounter     = 0;

                    foreach (var url in input.Urls)
                    {
                        string nameNExt = "";
                        if (url.Split('/').Length > 0)
                        {
                            nameNExt = url.Split('/')[url.Split('/').Length - 1];
                        }
                        else
                        {
                            return("URL is empty");
                        }

                        using (WebClient client = new WebClient())
                        {
                            nameNExt = Helpers.GetUniqueName(nameNExt, picPath);
                            client.DownloadFile(new Uri(url), picPath + nameNExt);

                            successCounter++;
                            curCounter++;
                            result += $"Picture {curCounter} successfully downloaded.\n";
                        }

                        Previewer.uploadPreview(picPath, previewPath, nameNExt);
                    }

                    return(result + $"Total pictures uploaded: {successCounter} from {curCounter}");
                }
                catch (Exception ex)
                {
                    //TODO: log error: ex.Message.ToString()
                    return("Error. Please, try again.");
                }
            }
            else
            {
                return("Unsucsessful uploading. Please, try again.");
            }
        }
Exemple #2
0
        public string Process(FileUpload input)
        {
            if (input?.Pictures?.Count > 0)
            {
                try
                {
                    string picPath     = _environment.WebRootPath + "\\Upload\\";
                    string previewPath = _environment.WebRootPath + "\\Preview\\";
                    if (!Directory.Exists(picPath))
                    {
                        Directory.CreateDirectory(picPath);
                    }
                    if (!Directory.Exists(previewPath))
                    {
                        Directory.CreateDirectory(previewPath);
                    }

                    string result         = "";
                    int    successCounter = 0;
                    int    curCounter     = 0;

                    foreach (var pic in input.Pictures)
                    {
                        string fileName = pic.FileName;
                        fileName = Helpers.GetUniqueName(fileName, picPath);

                        using (FileStream fileStream = System.IO.File.Create(picPath + fileName))
                        {
                            pic.CopyTo(fileStream);
                            fileStream.Flush();

                            successCounter++;
                            curCounter++;
                            result += $"Picture {curCounter} successfully downloaded.\n";
                        }
                        Previewer.uploadPreview(picPath, previewPath, fileName);
                    }
                    return(result + $"Total pictures uploaded: {successCounter} from {curCounter}");
                }
                catch (Exception ex)
                {
                    //TODO: log error: ex.Message.ToString()
                    //return "Error. Please, try again.";
                    return(ex.Message.ToString());
                }
            }
            else
            {
                return("Unsucsessful uploading. Please, try again.");
            }
        }
Exemple #3
0
        public void uploadPreviewTest()
        {
            //arrange
            string pathFrom = @"W:\CV\";
            string pathTo   = @"W:\CV\";
            string fileName = @"pic1.png";

            //act
            Previewer.uploadPreview(pathFrom, pathTo, fileName);

            //assert
            Assert.IsTrue(System.IO.File.Exists(@"W:\CV\preview_pic1.png"));

            //after test
            System.IO.File.Delete(@"W:\CV\preview_pic1.png");
        }
        public string Process(JsonElement input)
        {
            var json = input.ToString();

            if (String.IsNullOrWhiteSpace(json))
            {
                return("JSON is empty.");
            }

            List <Base64EncodedJSON> images;

            try
            {
                images = JsonConvert.DeserializeObject <List <Base64EncodedJSON> >(json);
            }
            catch (Exception ex)
            {
                return("Error in convetsation: " + ex.Message);
            }

            string result         = "";
            int    successCounter = 0;
            int    curCounter     = 0;

            if (images?.Count > 0)
            {
                try
                {
                    string picPath     = _environment.WebRootPath + "\\Upload\\";
                    string previewPath = _environment.WebRootPath + "\\Preview\\";
                    if (!Directory.Exists(picPath))
                    {
                        Directory.CreateDirectory(picPath);
                    }
                    if (!Directory.Exists(previewPath))
                    {
                        Directory.CreateDirectory(previewPath);
                    }

                    foreach (var pic in images)
                    {
                        string type = pic.GetType();
                        if (string.IsNullOrWhiteSpace(type))
                        {
                            result += $"Picture {curCounter} can not be loaded - because it have invalid image type.\r\n";
                            curCounter++;
                            continue;
                        }

                        string name   = pic.GetName();
                        var    base64 = pic.GetBase64();

                        string fileName = name + "." + type;
                        fileName = Helpers.GetUniqueName(fileName, picPath);

                        using (FileStream fileStream = System.IO.File.Create(picPath + fileName))
                        {
                            fileStream.Write(Convert.FromBase64String(base64));
                            fileStream.Flush();

                            successCounter++;
                            curCounter++;
                            result += $"Picture {curCounter} successfully downloaded.\n";
                        }
                        Previewer.uploadPreview(picPath, previewPath, fileName);
                    }
                    return(result + $"Total pictures uploaded: {successCounter} from {curCounter}");
                }
                catch (Exception ex)
                {
                    //TODO: log error: ex.Message.ToString()
                    return("Error. Please, try again.");
                }
            }
            else
            {
                return("Unsucsessful uploading. Please, try again.");
            }
        }