Esempio n. 1
0
        public async Task <HttpResponseMessage> PostFormData()
        {
            //Logger.Info("Starting novanet archives upload");

            if (!Request.Content.IsMimeMultipartContent())
            {
                Logger.Info("not IsMimeMultipartContent");
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            //get the query strings
            var qsCol = Request.RequestUri.ParseQueryString();

            var siteCode     = qsCol["siteCode"];
            var computerName = qsCol["computerName"];
            var fileName     = qsCol["fileName"];

            string novanetUploadPath     = ConfigurationManager.AppSettings["NovanetUploadPath"];
            string novanetFullUploadPath = Path.Combine(novanetUploadPath, siteCode, computerName);

            if (!Directory.Exists(novanetFullUploadPath))
            {
                Directory.CreateDirectory(novanetFullUploadPath);
                Logger.Info("Created folder for: " + novanetFullUploadPath);
            }

            string filePathName = Path.Combine(novanetFullUploadPath, fileName);
            var    fi           = new FileInfo(filePathName);

            if (fi.Exists)
            {
                //Logger.Info("File already exists on the server");
                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Conflict,
                    Content = new StringContent("Duplicate")
                });
            }
            var provider = new CustomMultipartFormDataStreamProvider(novanetFullUploadPath);

            try
            {
                //this gets the file stream form the request and saves to the folder
                await Request.Content.ReadAsMultipartAsync(provider);

                //Logger.Info("File uploaded to the server");
                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent("OK")
                });
            }
            catch (System.Exception e)
            {
                Logger.Info("api upload exception: " + e.Message);
                if (e.InnerException != null)
                {
                    Logger.Info("api upload inner exception: " + e.InnerException.Message);
                }
                Logger.Error("exception:", e.Message);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }
        public async Task<HttpResponseMessage> PostFormData()
        {
            Logger.Info("Starting checks upload");

            try
            {


                if (!Request.Content.IsMimeMultipartContent())
                {
                    Logger.Info("not IsMimeMultipartContent");
                    return new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.NotAcceptable,
                        Content = new StringContent("Bad key")
                    };
                }

                //get the query strings
                var qsCol = Request.RequestUri.ParseQueryString();

                var fileName = qsCol["fileName"];
                var siteCode = qsCol["siteCode"];
                //var key = qsCol["key"];

                //Check key - if bad return
                //if (!Utils.VerifyKey(key, fileName, siteCode))
                //{
                //    Logger.Info("ChecksUpload - bad key - file name: " + fileName + ", key: " + key );
                //    return new HttpResponseMessage
                //    {
                //        StatusCode = HttpStatusCode.NotAcceptable,
                //        Content = new StringContent("Bad key")
                //    };
                //}

                int retVal = Utils.IsStudyCleared(fileName);
                string msg = string.Empty;

                switch (retVal)
                {
                    case -1:
                        msg = "There was an error for checking if study id was cleared.";
                        break;
                    case 1:
                        msg = "Study id is cleared.";
                        break;
                    case 2:
                        msg = "Test studies are not uploaded.";
                        break;
                }

                if (retVal != 0)
                {
                    Logger.Info("ChecksUpload - " + msg + " - file name: " + qsCol["fileName"] + ", key: " + qsCol["key"]);
                    return new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.NotAcceptable,
                        Content = new StringContent(msg)
                    };
                }

                var savePath = GetSavePath(qsCol["siteCode"], qsCol["fileName"]);
                Logger.Info("Checks upload - file name:" + qsCol["fileName"]);

                //Logger.Info("Savepath:" + savePath);
                if (!Directory.Exists(savePath))
                {
                    Logger.Info("Creating savepath");
                    Directory.CreateDirectory(savePath);
                }

                //save the files in this folder
                string folder = savePath; //HttpContext.Current.Server.MapPath("~/App_Data");
                var provider = new CustomMultipartFormDataStreamProvider(folder);


                //this gets the file stream form the request and saves to the folder
                await Request.Content.ReadAsMultipartAsync(provider);

                // get the file info for uploaded file
                //var file = provider.FileData[0];
                //var fi = new FileInfo(file.LocalFileName);

                return new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent("OK")
                };

            }
            catch (System.Exception e)
            {
                Logger.Info("api upload exception: " + e.Message);
                if (e.InnerException != null)
                    Logger.Info("api upload inner exception: " + e.InnerException.Message);
                Logger.Error("exception:", e);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }
        public async Task <HttpResponseMessage> PostFormData()
        {
            Logger.Info("Starting checks upload");

            try
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    Logger.Info("not IsMimeMultipartContent");
                    return(new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.NotAcceptable,
                        Content = new StringContent("Bad key")
                    });
                }

                //get the query strings
                var qsCol = Request.RequestUri.ParseQueryString();

                var fileName = qsCol["fileName"];
                var siteCode = qsCol["siteCode"];
                //var key = qsCol["key"];

                //Check key - if bad return
                //if (!Utils.VerifyKey(key, fileName, siteCode))
                //{
                //    Logger.Info("ChecksUpload - bad key - file name: " + fileName + ", key: " + key );
                //    return new HttpResponseMessage
                //    {
                //        StatusCode = HttpStatusCode.NotAcceptable,
                //        Content = new StringContent("Bad key")
                //    };
                //}

                int    retVal = Utils.IsStudyCleared(fileName);
                string msg    = string.Empty;

                switch (retVal)
                {
                case -1:
                    msg = "There was an error for checking if study id was cleared.";
                    break;

                case 1:
                    msg = "Study id is cleared.";
                    break;

                case 2:
                    msg = "Test studies are not uploaded.";
                    break;
                }

                if (retVal != 0)
                {
                    Logger.Info("ChecksUpload - " + msg + " - file name: " + qsCol["fileName"] + ", key: " + qsCol["key"]);
                    return(new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.NotAcceptable,
                        Content = new StringContent(msg)
                    });
                }

                var savePath = GetSavePath(qsCol["siteCode"], qsCol["fileName"]);
                Logger.Info("Checks upload - file name:" + qsCol["fileName"]);

                //Logger.Info("Savepath:" + savePath);
                if (!Directory.Exists(savePath))
                {
                    Logger.Info("Creating savepath");
                    Directory.CreateDirectory(savePath);
                }

                //save the files in this folder
                string folder   = savePath; //HttpContext.Current.Server.MapPath("~/App_Data");
                var    provider = new CustomMultipartFormDataStreamProvider(folder);


                //this gets the file stream form the request and saves to the folder
                await Request.Content.ReadAsMultipartAsync(provider);

                // get the file info for uploaded file
                //var file = provider.FileData[0];
                //var fi = new FileInfo(file.LocalFileName);

                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent("OK")
                });
            }
            catch (System.Exception e)
            {
                Logger.Info("api upload exception: " + e.Message);
                if (e.InnerException != null)
                {
                    Logger.Info("api upload inner exception: " + e.InnerException.Message);
                }
                Logger.Error("exception:", e);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
        public async Task<HttpResponseMessage> PostFormData()
        {
            //Logger.Info("Starting novanet archives upload");

            if (!Request.Content.IsMimeMultipartContent())
            {
                Logger.Info("not IsMimeMultipartContent");
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            //get the query strings
            var qsCol = Request.RequestUri.ParseQueryString();

            var siteCode = qsCol["siteCode"];
            var computerName = qsCol["computerName"];
            var fileName = qsCol["fileName"];

            string logUploadPath = ConfigurationManager.AppSettings["LogUploadPath"];
            string logFullUploadPath = Path.Combine(logUploadPath, siteCode, computerName);

            if (!Directory.Exists(logFullUploadPath))
            {
                Directory.CreateDirectory(logFullUploadPath);
                Logger.Info("Created folder for: " + logFullUploadPath);
            }

            //string filePathName = Path.Combine(logFullUploadPath, fileName);
            //var fi = new FileInfo(filePathName);
            //if (fi.Exists)
            //{
            //    //Logger.Info("File already exists on the server");
            //    return new HttpResponseMessage
            //    {
            //        StatusCode = HttpStatusCode.Conflict,
            //        Content = new StringContent("Duplicate")
            //    };
            //}
            var provider = new CustomMultipartFormDataStreamProvider(logFullUploadPath);

            try
            {
                //this gets the file stream form the request and saves to the folder
                await Request.Content.ReadAsMultipartAsync(provider);

                //Logger.Info("File uploaded to the server");
                return new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StringContent("OK")
                };

            }
            catch (System.Exception e)
            {
                Logger.Info("api upload exception: " + e.Message);
                if (e.InnerException != null)
                    Logger.Info("api upload inner exception: " + e.InnerException.Message);
                Logger.Error("exception:", e.Message);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
            }
        }