示例#1
0
        public ActionResult FileUpload(MediaAssetUploadModel uploadedFileMeta)
        {
            var newImageId = new Guid();

            try
            {
                if (!GetImageMimeType(uploadedFileMeta.fileData.InputStream).Contains("unknown"))
                {
                    newImageId = ProcessUploadedImage(uploadedFileMeta);

                    Session["statusval"] = "OK";
                }
                else
                {
                    throw new Exception("Invalid image format");
                }
                //  newImageId = ProcessUploadedImage(uploadedFileMeta);

                //Session["statusval"] = "OK";
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Error processing image: {0}", ex.Message);
                Response.StatusCode = 500;
                Response.Write(errorMsg);
                return(Json(string.Empty));
            }

            return(Json(new { Id = newImageId, Status = "OK" }));
        }
示例#2
0
        /// <summary>
        /// Validate/Upload then delete the new dataSet. Email if there is an error
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DatasetUploaded(object sender, FileSystemEventArgs e)
        {
            const int maximumRetryPeriod = 5;     //max retry for 5 minutes
            const int retryDelay         = 10000; //try every 10 seconds

            var fileReceived = DateTime.Now;

            if (e.FullPath.Contains(".csv"))
            {
                try
                {
                    while (true) //have to wait until the file is fully uploaded
                    {
                        if (FileUploadCompleted(e.FullPath))
                        {
                            //get the title
                            var fileDetail   = e.FullPath.Split('\\');
                            var schemaId     = Convert.ToInt32(fileDetail[fileDetail.Length - 2]);
                            var schemaDetail = _dataSetSchemaService.Get(schemaId);
                            var fileName     = fileDetail[fileDetail.Length - 1];

                            //Validate and upload the data
                            var data = new MediaAssetUploadModel
                            {
                                SchemaId = schemaId,
                                Title    = String.Format("{0} at {1} {2}", schemaDetail.Title, DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString())
                            };

                            var result = _uploaderService.SaveCsv(schemaId, data.Title, e.FullPath);

                            if (result.Errors.Count > 0)
                            {
                                var email = BuildFailedUploadEmail(result, fileName, schemaDetail.Category.Title, schemaDetail.Title);
                                AddDebugInfo(new DebugInfo(String.Format("Category title:{0}, Schema title:{1}. Automatic csv upload failed as the csv had invalid data entered. File: {2} . Errors: {3}", schemaDetail.Category.Title, schemaDetail.Title, fileName, email.Length > 3500 ? email.Substring(0, 3500) : email), DebugInfoTypeEnum.FolderWatchTriggered));
                                SendEmail(schemaDetail.OwnerEmail, email, false, String.Format("Category title:{0}, Schema title:{1}. Automatic csv upload failed as the csv had invalid data entered", schemaDetail.Category.Title, schemaDetail.Title));
                            }
                            else
                            {
                                AddDebugInfo(new DebugInfo(String.Format(@"Category title:{0}, Schema title:{1}. Dataset was successfully added to datashare. Title: {2}", schemaDetail.Category.Title, schemaDetail.Title, data.Title), DebugInfoTypeEnum.FolderWatchTriggered));
                            }
                            break;
                        }
                        // Calculate the elapsed time and stop if the maximum retry period has been reached.
                        var timeElapsed = DateTime.Now - fileReceived;
                        if (timeElapsed.TotalMinutes > maximumRetryPeriod)
                        {
                            break;
                        }
                        Thread.Sleep(retryDelay);
                    }
                }
                catch (Exception ex)
                {
                    AddDebugInfo(new DebugInfo(String.Format(@"Error uploading dataset: {0}", ex.Message), DebugInfoTypeEnum.Error), ex);
                    RestartService("DataShare.Service", 10000);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Processes the uploaded image.
        /// </summary>
        /// <param name="uploadedFileMeta">The uploaded file meta.</param>
        /// <returns>Image Id</returns>
        private Guid ProcessUploadedImage(MediaAssetUploadModel uploadedFileMeta)
        {
            // Get the file extension
            WorkingImageExtension = Path.GetExtension(uploadedFileMeta.Filename).ToLower();
            string[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" }; // Make sure it is an image that can be processed
            if (allowedExtensions.Contains(WorkingImageExtension))
            {
                WorkingImageId = Guid.NewGuid();
                Image workingImage = new Bitmap(uploadedFileMeta.fileData.InputStream);
                WorkingImage = ImageHelperNew.ImageToByteArray(workingImage);
            }
            else
            {
                throw new Exception("Cannot process files of this type.");
            }

            return(WorkingImageId);
        }
示例#4
0
        public ActionResult FileUpload(MediaAssetUploadModel uploadedFileMeta)
        {
            Guid newImageId = new Guid();

            try
            {
                newImageId = ProcessUploadedImage(uploadedFileMeta);
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Error processing image: {0}", ex.Message);
                Response.StatusCode = 500;
                Response.Write(errorMsg);
                return(Json(string.Empty));
            }

            return(Json(new { Id = newImageId, Status = "OK" }));
        }