示例#1
0
        public async Task <IActionResult> OnPostAsync()
        {
            try {
                ViewData["ExceptionMessage"] = "No Exceptions";

                if (!ModelState.IsValid)
                {
                    setViewData();
                    return(Page());
                }

                var project = await _context.ge_project
                              .FirstOrDefaultAsync(p => p.Id == data.projectId);

                if (project == null)
                {
                    setViewData();
                    return(Page());
                }

                data.project = project;

                var UserAuthorised = await IsUserCreateAuthorised();

                if (IsCreateAllowed() != geOPSResp.Allowed)
                {
                    return(RedirectToPageMessage(msgCODE.DATA_CREATE_PROHIBITED));
                }
                if (!UserAuthorised.Succeeded)
                {
                    return(RedirectToPageMessage(msgCODE.DATA_CREATE_USER_PROHIBITED));
                }

                if (!UpdateProjectionLoc(data))
                {
                    setViewData();
                    return(Page());
                }

                if (String.IsNullOrEmpty(LastModifiedDates))
                {
                    setViewData();
                    return(Page());
                }



                string[] lastmodified = LastModifiedDates.Split(";");
                int      i            = 0;

                long MaxFileSize      = _config.Value.defaultMaxFileSize;
                int  DbCommandTimeout = Int32.Parse(_config.Value.defaultEFDBTimeOut);

                ge_MimeTypes mtypes = new ge_MimeTypes();

                foreach (var formFile in uploadFiles)
                {
                    Boolean IsContentText = formFile.IsContentTypeText(true);

                    ge_data_file b = new ge_data_file();
                    ge_data      d = new ge_data();

                    d.createdId = GetUserIdAsync().Result;

                    if (IsContentText)
                    {
                        Boolean IsContentXML = formFile.IsContentTypeXML();
                        if (IsContentXML)
                        {
                            b.data_xml = await formFile.ProcessFormFileString(ModelState, MaxFileSize, Encoding.UTF8, true);

                            d.SetEncoding(Encoding.UTF8);
                        }
                        else
                        {
                            Encoding encoding = formFile.ReadEncoding(Encoding.UTF8);
                            b.data_string = await formFile.ProcessFormFileString(ModelState, MaxFileSize, encoding, false);

                            d.SetEncoding(encoding);
                        }
                    }
                    else
                    {
                        b.data_binary = await formFile.ProcessFormFileBinary(ModelState, MaxFileSize);

                        d.SetEncoding(null);
                    }

                    // Perform a second check to catch ProcessFormFile method
                    // violations.
                    if (!ModelState.IsValid)
                    {
                        return(Page());
                    }

                    d.projectId = project.Id;

                    d.locEast  = data.locEast;
                    d.locNorth = data.locNorth;
                    d.locLevel = data.locLevel;

                    d.locLatitude  = data.locLatitude;
                    d.locLongitude = data.locLongitude;
                    d.locHeight    = data.locHeight;

                    d.locMapReference = data.locMapReference;
                    d.locName         = data.locName;
                    d.locAddress      = data.locAddress;
                    d.locPostcode     = data.locPostcode;

                    d.datumProjection = data.datumProjection;

                    d.description = data.description;
                    d.keywords    = data.keywords;
                    d.operations  = data.operations;

                    // Add deatils of uploaded file to new _ge_data record
                    d.file     = b;
                    d.filesize = formFile.Length;
                    d.filename = getFilenameNoPath(formFile.FileName);
                    d.fileext  = formFile.FileExtension();

                    if (mtypes.ContainsKey(d.fileext))
                    {
                        d.filetype = mtypes[d.fileext];
                    }
                    else
                    {
                        d.filetype = formFile.ContentType;
                    }

                    if (IsDateTimeFormat(lastmodified[i]))
                    {
                        d.filedate = Convert.ToDateTime(lastmodified[i]);
                    }

                    if (!UpdateCreated(d))
                    {
                        return(Page());
                    }

                    if (!UpdateLastEdited(d))
                    {
                        return(Page());
                    }

                    _context.ge_data.Add(d);

                    i++;
                }



                if (DbCommandTimeout > 0)
                {
                    _context.Database.SetCommandTimeout(DbCommandTimeout);
                }

                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index", new { projectId = data.projectId }));
            } catch (Exception e) {
                String msg = e.Message + e.InnerException.Message;
                ViewData["ExceptionMessage"] = "Exception: " + msg;
                return(Page());
            }
        }
示例#2
0
        public async Task <int> CreateData(List <IFormFile> uploadFiles, string[] lastmodified, ge_data template, ModelStateDictionary ModelState, int MaxFileSize)
        {
            ge_MimeTypes mtypes = new ge_MimeTypes();

            int last_modified_offset = 0;

            foreach (var formFile in uploadFiles)
            {
                ge_data      d = new ge_data();
                ge_data_file f;

                d.createdId = template.createdId;
                d.createdDT = DateTime.UtcNow;

                d.editedId = template.editedId;
                d.editedDT = DateTime.UtcNow;

                Boolean IsContentText = formFile.IsContentTypeText(true);

                if (IsContentText)
                {
                    Boolean IsContentXML = formFile.IsContentTypeXML();
                    if (IsContentXML)
                    {
                        f = await _unitOfWork.Data.GetDataFileString(formFile, ModelState, MaxFileSize, Encoding.UTF8, true);

                        d.SetEncoding(Encoding.UTF8);
                    }
                    else
                    {
                        Encoding encoding = formFile.ReadEncoding(Encoding.UTF8);
                        f = await _unitOfWork.Data.GetDataFileString(formFile, ModelState, MaxFileSize, encoding, false);

                        d.SetEncoding(encoding);
                    }
                }
                else
                {
                    f = await _unitOfWork.Data.GetDataFileBinary(formFile, ModelState, MaxFileSize);

                    d.SetEncoding(null);
                }

                // Perform a second check to catch ProcessFormFile method
                // violations.
                if (!ModelState.IsValid)
                {
                    return(-1);
                }

                d.projectId = template.Id;

                d.locEast  = template.locEast;
                d.locNorth = template.locNorth;
                d.locLevel = template.locLevel;

                d.locLatitude  = template.locLatitude;
                d.locLongitude = template.locLongitude;
                d.locHeight    = template.locHeight;

                d.locMapReference = template.locMapReference;
                d.locName         = template.locName;
                d.locAddress      = template.locAddress;
                d.locPostcode     = template.locPostcode;

                d.datumProjection = template.datumProjection;

                d.description = template.description;
                d.keywords    = template.keywords;
                d.operations  = template.operations;

                // Add deatils of uploaded file to new _ge_data record
                d.file     = f;
                d.filesize = formFile.Length;
                d.filename = formFile.FilenameNoPath();
                d.fileext  = formFile.FileExtension();

                if (mtypes.ContainsKey(d.fileext))
                {
                    d.filetype = mtypes[d.fileext];
                }
                else
                {
                    d.filetype = formFile.ContentType;
                }

                if (lastmodified[last_modified_offset].IsDateTimeFormat())
                {
                    d.filedate = Convert.ToDateTime(lastmodified[last_modified_offset]);
                }

                await _unitOfWork.Data.AddAsync(d);

                last_modified_offset++;
            }

            return(last_modified_offset);
        }
示例#3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var project = await _context.ge_project
                          .FirstOrDefaultAsync(m => m.Id == data.projectId);

            if (project == null)
            {
                return(Page());
            }

            data.project = project;

            int isUpdateAllowed = IsUpdateAllowed();
            var UserAuthorised  = await IsUserUpdateAuthorised();

            if (isUpdateAllowed != geOPSResp.Allowed)
            {
                if (isUpdateAllowed == geOPSResp.Data && !IsUserAdmin())
                {
                    return(RedirectToPageMessage(msgCODE.DATA_UPDATE_PROHIBITED));
                }
                if (isUpdateAllowed == geOPSResp.ProjectData && !IsUserAdmin())
                {
                    return(RedirectToPageMessage(msgCODE.DATA_UPDATE_PROHIBITED));
                }
                if (isUpdateAllowed == geOPSResp.DataApproved && !IsUserApprover())
                {
                    return(RedirectToPageMessage(msgCODE.DATA_UPDATE_PROHIBITED));
                }
                if (isUpdateAllowed == geOPSResp.ProjectApproved && !IsUserApprover())
                {
                    return(RedirectToPageMessage(msgCODE.DATA_UPDATE_PROHIBITED));
                }
            }

            if (!UserAuthorised.Succeeded && !IsUserAdmin())
            {
                return(RedirectToPageMessage(msgCODE.DATA_UPDATE_USER_PROHIBITED));
            }

            if (!UpdateProjectionLoc(data))
            {
                return(Page());
            }

            if (!UpdateLastEdited(data))
            {
                return(Page());
            }

            long MaxFileSize      = _config.Value.defaultMaxFileSize;
            int  DbCommandTimeout = Int32.Parse(_config.Value.defaultEFDBTimeOut);

            if (uploadFiles.Count > 0)
            {
                ge_MimeTypes mtypes       = new ge_MimeTypes();
                string[]     lastmodified = LastModifiedDates.Split(";");
                var          formFile     = uploadFiles[0];
                var          b            = await _context.ge_data_file.FirstOrDefaultAsync(m => m.Id == data.Id);

                Boolean IsContentText = formFile.IsContentTypeText(true);

                if (IsContentText)
                {
                    Boolean IsContentXML = formFile.IsContentTypeXML();
                    if (IsContentXML)
                    {
                        b.data_xml = await formFile.ProcessFormFileString(ModelState, MaxFileSize, Encoding.UTF8, true);

                        b.data_string = null;
                        b.data_binary = null;
                        data.SetEncoding(Encoding.UTF8);
                    }
                    else
                    {
                        Encoding encoding = formFile.ReadEncoding(Encoding.UTF8);
                        b.data_binary = null;
                        b.data_xml    = null;
                        b.data_string = await formFile.ProcessFormFileString(ModelState, MaxFileSize, encoding, false);

                        data.SetEncoding(encoding);
                    }
                }
                else
                {
                    b.data_xml    = null;
                    b.data_string = null;
                    b.data_binary = await formFile.ProcessFormFileBinary(ModelState, MaxFileSize);

                    data.SetEncoding(null);
                }

                if (!ModelState.IsValid)
                {
                    return(Page());
                }

                data.file     = b;
                data.filesize = formFile.Length;
                data.filename = getFilenameNoPath(formFile.FileName);
                data.fileext  = formFile.FileExtension();

                if (mtypes.ContainsKey(data.fileext))
                {
                    data.filetype = mtypes[data.fileext];
                }
                else
                {
                    data.filetype = formFile.ContentType;
                }

                if (IsDateTimeFormat(lastmodified[0]))
                {
                    data.filedate = Convert.ToDateTime(lastmodified[0]);
                }
            }

            _context.Attach(data).State = EntityState.Modified;

            try {
                if (DbCommandTimeout > 0)
                {
                    _context.Database.SetCommandTimeout(DbCommandTimeout);
                }

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) {
                if (!dataExists(data.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            catch (Exception e) {
                String msg = e.Message + e.InnerException.Message;
                ViewData["ExceptionMessage"] = "Exception: " + msg;
                return(Page());
            }

            return(RedirectToPage("./Index", new { projectId = data.projectId }));
        }