public Task<HttpResponseMessage> UploadImportFile() { logger.Debug("starting to process incoming files."); if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = System.Web.HttpContext.Current.Server.MapPath("~/uploads"); string rootUrl = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, String.Empty); logger.Debug("saving files to location: " + root); logger.Debug(" and the root url = " + rootUrl); var provider = new MultipartFormDataStreamProvider(root); User me = AuthorizationManager.getCurrentUser(); var db = ServicesContext.Current; var task = Request.Content.ReadAsMultipartAsync(provider). ContinueWith<HttpResponseMessage>(o => { if (o.IsFaulted || o.IsCanceled) { logger.Debug("Error: " + o.Exception.Message); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, o.Exception)); } //Look up our project Int32 ProjectId = Convert.ToInt32(provider.FormData.Get("ProjectId")); logger.Debug("And we think the projectid === " + ProjectId); Project project = db.Projects.Find(ProjectId); if (!project.isOwnerOrEditor(me)) throw new Exception("Authorization error."); var newFileName = ""; foreach (MultipartFileData file in provider.FileData) { logger.Debug("Filename = " + file.LocalFileName); logger.Debug("Orig = " + file.Headers.ContentDisposition.FileName); logger.Debug("Name? = " + file.Headers.ContentDisposition.Name); var fileIndex = ActionController.getFileIndex(file.Headers.ContentDisposition.Name); //"uploadedfile0" -> 0 var filename = file.Headers.ContentDisposition.FileName; filename = filename.Replace("\"", string.Empty); if (!String.IsNullOrEmpty(filename)) { try { newFileName = ActionController.relocateProjectFile( file.LocalFileName, ProjectId, filename, true); /* File newFile = new File(); newFile.Title = provider.FormData.Get("Title_" + fileIndex); //"Title_1, etc. newFile.Description = provider.FormData.Get("Description_" + fileIndex); //"Description_1, etc. newFile.Name = info.Name;//.Headers.ContentDisposition.FileName; newFile.Link = rootUrl + "/services/uploads/" + ProjectId + "/" + info.Name; //file.LocalFileName; newFile.Size = (info.Length / 1024).ToString(); //file.Headers.ContentLength.ToString(); newFile.FileTypeId = FileType.getFileTypeFromFilename(info); newFile.UserId = me.Id; logger.Debug(" Adding file " + newFile.Name + " at " + newFile.Link); files.Add(newFile); */ } catch (Exception e) { logger.Debug("Error: " + e.ToString()); } } } logger.Debug("Done saving files."); ImportDataResult data = new ImportDataResult(); var info = new System.IO.FileInfo(newFileName); //process the file and return all the data! //TODO: refactor this into import plugins via polymorphism. ... but maybe this is enough. :) //CSV or Excel are the only filetypes currently supported. if (info.Extension == ".xls" || info.Extension == ".xlsx") { logger.Debug("Looks like an excel file!"); ExcelReader reader = new ExcelReader(newFileName); //ExcelReader doesn't support starting on a certain line for column names... we always assume col 1 data.columns = reader.getColumns(); data.rows = reader.getData().First().Table; reader.close(); } else if (info.Extension == ".csv") { logger.Debug("Looks like a csv file!"); Int32 StartOnLine = Convert.ToInt32(provider.FormData.Get("StartOnLine")); //only applicable to T/CSV CSVReader reader = new CSVReader(newFileName); data = reader.getImportDataResult(StartOnLine); // we do it all in one. } else if (info.Extension == ".tsv") { logger.Debug("Looks like a tsv file!"); Int32 StartOnLine = Convert.ToInt32(provider.FormData.Get("StartOnLine")); //only applicable to T/CSV TSVReader reader = new TSVReader(newFileName); data = reader.getImportDataResult(StartOnLine); // we do it all in one. } else { logger.Debug("Looks like an unknown file!"); throw new Exception("File type not compatible. We can do Excel (xls/xslx), CSV (csv) and TSV (tsv)."); } string result = JsonConvert.SerializeObject(data); //TODO: actual error/success message handling //string result = "{\"message\": \"Success\"}"; HttpResponseMessage resp = new HttpResponseMessage(System.Net.HttpStatusCode.OK); resp.Content = new System.Net.Http.StringContent(result, System.Text.Encoding.UTF8, "text/plain"); //to stop IE from being stupid. return resp; }); return task; }
public Task<HttpResponseMessage> UploadImportFile() { logger.Debug("Inside DataActionController, UploadImportFile..."); logger.Debug("starting to process incoming files."); if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = System.Web.HttpContext.Current.Server.MapPath("~/uploads"); string rootUrl = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, String.Empty); logger.Debug("saving files to location: " + root); logger.Debug(" and the root url = " + rootUrl); var provider = new MultipartFormDataStreamProvider(root); User me = AuthorizationManager.getCurrentUser(); var db = ServicesContext.Current; var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(o => { if (o.IsFaulted || o.IsCanceled) { logger.Debug("Error: " + o.Exception.Message); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, o.Exception)); } //Look up our project Int32 ProjectId = Convert.ToInt32(provider.FormData.Get("ProjectId")); logger.Debug("And we think the projectid === " + ProjectId); Project project = db.Projects.Find(ProjectId); logger.Debug("Project = " + project); if (!project.isOwnerOrEditor(me)) throw new Exception("Authorization error: The user trying to import is neither an Owner nor an Editor."); else logger.Debug("User authorized = " + me); var newFileName = ""; foreach (MultipartFileData file in provider.FileData) { logger.Debug("Filename = " + file.LocalFileName); logger.Debug("Orig = " + file.Headers.ContentDisposition.FileName); logger.Debug("Name? = " + file.Headers.ContentDisposition.Name); var fileIndex = ActionController.getFileIndex(file.Headers.ContentDisposition.Name); //"uploadedfile0" -> 0 var filename = file.Headers.ContentDisposition.FileName; filename = filename.Replace("\"", string.Empty); if (!String.IsNullOrEmpty(filename)) { try { newFileName = ActionController.relocateProjectFile( file.LocalFileName, ProjectId, filename, true); /* File newFile = new File(); newFile.Title = provider.FormData.Get("Title_" + fileIndex); //"Title_1, etc. newFile.Description = provider.FormData.Get("Description_" + fileIndex); //"Description_1, etc. newFile.Name = info.Name;//.Headers.ContentDisposition.FileName; newFile.Link = rootUrl + "/services/uploads/" + ProjectId + "/" + info.Name; //file.LocalFileName; newFile.Size = (info.Length / 1024).ToString(); //file.Headers.ContentLength.ToString(); newFile.FileTypeId = FileType.getFileTypeFromFilename(info); newFile.UserId = me.Id; logger.Debug(" Adding file " + newFile.Name + " at " + newFile.Link); files.Add(newFile); */ } catch (Exception e) { logger.Debug("Error: " + e.ToString()); } } } logger.Debug("Done saving files."); var data = new ImportDataResult(); var info = new FileInfo(newFileName); // Process the file and return all the data! /* Note: According to Colette, if someone tries to upload a file with an odd extension (.lkg, .fld, MCR, BC1, etc.), * while the extension may vary, it will almost always be a ScrewTrap-PITAGIS related file. * Therefore, we are allowing a wide range of variation in the extensions. */ //var regex = new Regex(@"\.(m|r|ur|mc)\d+$"); //var regexNums = new Regex(@"\.(m|r|ur|mc|bc)\d+$"); //var regexChars = new Regex(@"\.(m|r|ur|mc|bc)\D+$"); var regexNums = new Regex(@"\.(m|r|ur|mc|bc|nb)\d+$"); var regexChars = new Regex(@"\.(m|r|ur|mc|bc|nb)\D+$"); var extension = info.Extension.ToLower(); logger.Debug("extension = " + extension); if (extension == ".xls" || extension == ".xlsx") { logger.Debug("Looks like an excel file!"); var reader = new ExcelReader(newFileName); //ExcelReader doesn't support starting on a certain line for column names... we always assume col 1 data.columns = reader.getColumns(); data.rows = reader.getData().First().Table; reader.close(); } else if (extension == ".csv") { logger.Debug("Looks like a csv file!"); var StartOnLine = Convert.ToInt32(provider.FormData.Get("StartOnLine")); //only applicable to T/CSV var reader = new CSVReader(newFileName); data = reader.getImportDataResult(StartOnLine); // we do it all in one. } else if (extension == ".tsv") { logger.Debug("Looks like a tsv file!"); var StartOnLine = Convert.ToInt32(provider.FormData.Get("StartOnLine")); //only applicable to T/CSV var reader = new TSVReader(newFileName); data = reader.getImportDataResult(StartOnLine); // we do it all in one. } //else if (extension == ".lkg" || extension == ".fld" || regex.Match(extension).Success) else if (extension == ".lkg" || extension == ".fld" || regexNums.Match(extension).Success || regexChars.Match(extension).Success) { logger.Debug("Looks like a PITAGIS file!"); var reader = new PitagisReader(newFileName); data = reader.getImportDataResult(); // we do it all in one. } else { logger.Debug("Looks like an unknown file!"); throw new Exception("File type not compatible. We can do Excel (xls/xslx), CSV (csv), TSV (tsv), and PITAGIS (.lkg/.fld/.m01/.r01/.ur1/.mc1)."); } var result = JsonConvert.SerializeObject(data); //TODO: actual error/success message handling //string result = "{\"message\": \"Success\"}"; var resp = new HttpResponseMessage(HttpStatusCode.OK); resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain"); //to stop IE from being stupid. return resp; }); return task; }