Exemple #1
0
        private void UploadIndices(S3FileTransfer s3FileTransfer, string s3FileName, string downloadLocalPath, string siteModelUid)
        {
            var filename          = SpatialIndexFileName(s3FileName);
            var spatialUploadedOk = s3FileTransfer.WriteFile(downloadLocalPath, Guid.Parse(siteModelUid), filename);

            if (!spatialUploadedOk)
            {
                throw new ArgumentException($"Unable to copy spatial index file to S3: {filename}");
            }
            filename = SubGridIndexFileName(s3FileName);
            var subgridUploadedOk = s3FileTransfer.WriteFile(downloadLocalPath, Guid.Parse(siteModelUid), filename);

            if (!subgridUploadedOk)
            {
                throw new ArgumentException($"Unable to copy subgrid index file to S3: {filename}");
            }
            // upload boundary...
            filename = BoundaryFileName(s3FileName);
            var boundaryUploadedOk = s3FileTransfer.WriteFile(downloadLocalPath, Guid.Parse(siteModelUid), filename);

            if (!boundaryUploadedOk)
            {
                throw new ArgumentException($"Unable to copy boundary file to S3: {filename}");
            }
        }
Exemple #2
0
        public void FileTransfer_HappyPath()
        {
            var mockTransferProxy = new Mock <ITransferProxy>();

            mockTransferProxy.Setup(t => t.Upload(It.IsAny <Stream>(), It.IsAny <string>()));

            var mockTransferProxyFactory = new Mock <ITransferProxyFactory>();

            mockTransferProxyFactory.Setup(x => x.NewProxy(It.IsAny <TransferProxyType>())).Returns(mockTransferProxy.Object);

            var mockConfig = new Mock <IConfigurationStore>();

            mockConfig.Setup(x => x.GetValueString("AWS_DESIGNIMPORT_BUCKET_NAME")).Returns("vss-projects-stg");

            DIBuilder
            .New()
            .AddLogging()
            .Add(x => x.AddSingleton(mockConfig.Object))
            .Add(x => x.AddSingleton(mockTransferProxyFactory.Object))
            .Complete();

            var projectUid       = Guid.Parse("A11F2458-6666-424F-A995-4426a00771AE");
            var transferFileName = "TransferTestDesign.ttm";

            var s3FileTransfer  = new S3FileTransfer(TransferProxyType.DesignImport);
            var isWrittenToS3Ok = s3FileTransfer.WriteFile("TestData", projectUid, transferFileName);

            Assert.True(isWrittenToS3Ok);
        }
Exemple #3
0
        public async Task <CompactionExportResult> PostTINSurface([FromBody] CompactionSurfaceExportRequest compactionSurfaceExportRequest)
        {
            Log.LogInformation($"{nameof(PostTINSurface)}: {Request.QueryString}");

            Log.LogDebug($"Accept header is {Request.Headers[HeaderConstants.ACCEPT]}");

            compactionSurfaceExportRequest.Validate();
            ValidateFilterMachines(nameof(PostTINSurface), compactionSurfaceExportRequest.ProjectUid, compactionSurfaceExportRequest.Filter);

            var tinResult = await WithServiceExceptionTryExecuteAsync(() =>
                                                                      RequestExecutorContainer
                                                                      .Build <TINSurfaceExportExecutor>(ConfigStore, LoggerFactory, ServiceExceptionHandler)
                                                                      .ProcessAsync(compactionSurfaceExportRequest)) as TINSurfaceExportResult;

            if (tinResult?.TINData == null || tinResult?.Code != ContractExecutionStatesEnum.ExecutedSuccessfully)
            {
                return(new CompactionExportResult(tinResult.Code, tinResult.Message));
            }

            const string TTM_EXTENSION = ".ttm";
            const string ZIP_EXTENSION = ".zip";

            var fullFileName = BuildTINFilePath(compactionSurfaceExportRequest.FileName, ZIP_EXTENSION);

            if (FileSystem.Exists(fullFileName))
            {
                FileSystem.Delete(fullFileName);
            }

            using (var zipFile = ZipFile.Open(fullFileName, ZipArchiveMode.Create))
            {
                var entry = zipFile.CreateEntry(compactionSurfaceExportRequest.FileName + TTM_EXTENSION);
                using (var stream = entry.Open())
                    new MemoryStream(tinResult?.TINData).CopyTo(stream);
            }

            var s3FileTransfer = new S3FileTransfer(TransferProxyType.Temporary);

            s3FileTransfer.WriteFile(fullFileName, compactionSurfaceExportRequest.ProjectUid, out var url);
            if (FileSystem.Exists(fullFileName)) // remove temp file
            {
                FileSystem.Delete(fullFileName);
            }
            return(new CompactionExportResult(url));
        }
Exemple #4
0
        /// <summary>
        /// Add a design or surveyed surface or alignment file to TRex
        /// </summary>
        protected async Task AddDesign(DesignRequest request, string executorName)
        {
            // load core file from s3 to local
            var localPath            = FilePathHelper.GetTempFolderForProject(request.ProjectUid);
            var localPathAndFileName = Path.Combine(new[] { localPath, request.FileName });

            DesignBase design;

            if (request.FileType == ImportedFileType.Alignment)
            {
                design = new SVLAlignmentDesign();
            }
            else
            {
                design = new TTMDesign(SubGridTreeConsts.DefaultCellSize);
            }
            var designLoadResult = design.LoadFromStorage(request.ProjectUid, request.FileName, localPath);

            if (designLoadResult != DesignLoadResult.Success)
            {
                log.LogError($"#Out# {executorName}. Addition of design failed :{request.FileName}, Project:{request.ProjectUid}, DesignUid:{request.DesignUid}, designLoadResult: {designLoadResult}");
                throw CreateServiceException <TExecutor>
                          (HttpStatusCode.InternalServerError, ContractExecutionStatesEnum.InternalProcessingError,
                          RequestErrorStatus.DesignImportUnableToRetrieveFromS3, designLoadResult.ToString());
            }

            if (request.FileType != ImportedFileType.Alignment)
            {
                // This generates the 2 index files
                designLoadResult = design.LoadFromFile(localPathAndFileName);
                if (designLoadResult != DesignLoadResult.Success)
                {
                    log.LogError($"#Out# {executorName}. Addition of design failed :{request.FileName}, Project:{request.ProjectUid}, DesignUid:{request.DesignUid}, designLoadResult: {designLoadResult}");
                    throw CreateServiceException <TExecutor>
                              (HttpStatusCode.InternalServerError, ContractExecutionStatesEnum.InternalProcessingError,
                              RequestErrorStatus.DesignImportUnableToCreateDesign, designLoadResult.ToString());
                }
            }

            var extents = new BoundingWorldExtent3D();

            design.GetExtents(out extents.MinX, out extents.MinY, out extents.MaxX, out extents.MaxY);
            design.GetHeightRange(out extents.MinZ, out extents.MaxZ);

            if (request.FileType == ImportedFileType.DesignSurface)
            {
                // Create the new designSurface in our site
                var tRexRequest      = new AddTTMDesignRequest();
                var designSurfaceUid = await tRexRequest.ExecuteAsync(new AddTTMDesignArgument
                {
                    ProjectID        = request.ProjectUid,
                    DesignDescriptor = new Designs.Models.DesignDescriptor(request.DesignUid, localPathAndFileName, request.FileName),
                    Extents          = extents,
                    ExistenceMap     = design.SubGridOverlayIndex()
                });
            }

            if (request.FileType == ImportedFileType.SurveyedSurface)
            {
                // Create the new SurveyedSurface in our site model
                var tRexRequest        = new AddSurveyedSurfaceRequest();
                var surveyedSurfaceUid = await tRexRequest.ExecuteAsync(new AddSurveyedSurfaceArgument
                {
                    ProjectID        = request.ProjectUid,
                    DesignDescriptor = new Designs.Models.DesignDescriptor(request.DesignUid, localPathAndFileName, request.FileName),
                    AsAtDate         = request.SurveyedUtc ?? TRex.Common.Consts.MIN_DATETIME_AS_UTC, // validation will have ensured this exists
                    Extents          = extents,
                    ExistenceMap     = design.SubGridOverlayIndex()
                });
            }

            if (request.FileType == ImportedFileType.Alignment)
            {
                // Create the new alignment in our site model
                var tRexRequest  = new AddAlignmentRequest();
                var alignmentUid = await tRexRequest.ExecuteAsync(new AddAlignmentArgument
                {
                    ProjectID        = request.ProjectUid,
                    DesignDescriptor = new Designs.Models.DesignDescriptor(request.DesignUid, localPathAndFileName, request.FileName),
                    Extents          = extents
                });
            }

            if (request.FileType != ImportedFileType.Alignment)
            {
                //  TTM.LoadFromFile() will have created these 3 files. We need to store them on S3 to reload cache when required
                var s3FileTransfer = new S3FileTransfer(TransferProxyType.DesignImport);
                s3FileTransfer.WriteFile(localPath, request.ProjectUid, SubGridIndexFileName(request.FileName));
                s3FileTransfer.WriteFile(localPath, request.ProjectUid, SpatialIndexFileName(request.FileName));
                s3FileTransfer.WriteFile(localPath, request.ProjectUid, BoundaryFileName(request.FileName));
            }
        }
Exemple #5
0
        public async Task <JsonResult> AddDesignToSiteModel(
            string siteModelUid,
            string importedFileType,
            [FromQuery] string fileNameAndLocalPath,
            [FromQuery] string asAtDate,
            [FromQuery] Guid designUid)
        {
            var importedFileTypeEnum = ValidateImportedFileType(importedFileType);

            if (string.IsNullOrEmpty(siteModelUid))
            {
                throw new ArgumentException($"Invalid siteModelUid (you need to have selected one first): {siteModelUid}");
            }

            if (string.IsNullOrEmpty(fileNameAndLocalPath) ||
                !Path.HasExtension(fileNameAndLocalPath) ||
                (importedFileTypeEnum != ImportedFileType.Alignment &&
                 (string.Compare(Path.GetExtension(fileNameAndLocalPath), ".ttm", StringComparison.OrdinalIgnoreCase) != 0))
                ||
                (importedFileTypeEnum == ImportedFileType.Alignment &&
                 (string.Compare(Path.GetExtension(fileNameAndLocalPath), ".svl", StringComparison.OrdinalIgnoreCase) != 0))
                )
            {
                throw new ArgumentException($"Invalid [path]filename: {fileNameAndLocalPath}");
            }

            if (!System.IO.File.Exists(fileNameAndLocalPath))
            {
                throw new ArgumentException($"Unable to locate [path]fileName: {fileNameAndLocalPath}");
            }

            var siteModelGuid = Guid.Parse(siteModelUid);

            if (designUid == Guid.Empty)
            {
                designUid = Guid.NewGuid();
            }

            // copy local file to S3
            bool   designFileLoadedOk;
            string s3FileName;
            var    destinationFileName = string.Empty;
            var    tempFileNameOnly    = Path.GetFileName(fileNameAndLocalPath);

            var s3FileTransfer = new S3FileTransfer(TransferProxyType.DesignImport);

            if (importedFileTypeEnum == ImportedFileType.SurveyedSurface)
            {
                var tempDate = asAtDate.Remove(asAtDate.IndexOf(".", 0, StringComparison.Ordinal)).Replace(":", "");
                destinationFileName = Path.GetFileNameWithoutExtension(tempFileNameOnly) + $"_{tempDate}" + Path.GetExtension(tempFileNameOnly);

                designFileLoadedOk = s3FileTransfer.WriteFile(Path.GetDirectoryName(fileNameAndLocalPath), Guid.Parse(siteModelUid), tempFileNameOnly, destinationFileName);

                s3FileName = destinationFileName;
            }
            else
            {
                designFileLoadedOk = s3FileTransfer.WriteFile(Path.GetDirectoryName(fileNameAndLocalPath), Guid.Parse(siteModelUid), tempFileNameOnly);
                s3FileName         = tempFileNameOnly;
            }

            if (!designFileLoadedOk)
            {
                throw new ArgumentException($"Unable to copy design file to S3: {s3FileName}");
            }

            // download to appropriate local location and add to site model
            var downloadLocalPath = FilePathHelper.GetTempFolderForProject(Guid.Parse(siteModelUid));
            var downloadedok      = await s3FileTransfer.ReadFile(Guid.Parse(siteModelUid), s3FileName, downloadLocalPath).ConfigureAwait(false);

            if (!downloadedok)
            {
                throw new ArgumentException($"Unable to restore same design file from S3: {s3FileName}");
            }

            if (importedFileTypeEnum == ImportedFileType.DesignSurface)
            {
                AddTheDesignSurfaceToSiteModel(siteModelGuid, designUid, downloadLocalPath, s3FileName);

                // upload indices
                UploadIndices(s3FileTransfer, s3FileName, downloadLocalPath, siteModelUid);

                return(new JsonResult(DIContext.Obtain <IDesignManager>().List(siteModelGuid).Locate(designUid)));
            }

            if (importedFileTypeEnum == ImportedFileType.SurveyedSurface)
            {
                var surveyedUtc = DateTime.Parse(asAtDate).ToUniversalTime(); //DateTime.UtcNow; // unable to parse the date from UI DateTime.Parse(asAtDate);
                AddTheSurveyedSurfaceToSiteModel(siteModelGuid, designUid, downloadLocalPath, s3FileName, surveyedUtc);

                // upload indices
                UploadIndices(s3FileTransfer, s3FileName, downloadLocalPath, siteModelUid);

                return(new JsonResult(DIContext.Obtain <ISurveyedSurfaceManager>().List(siteModelGuid).Locate(designUid)));
            }

            if (importedFileTypeEnum == ImportedFileType.Alignment)
            {
                AddTheAlignmentToSiteModel(siteModelGuid, designUid, downloadLocalPath, s3FileName);
                return(new JsonResult(DIContext.Obtain <IAlignmentManager>().List(siteModelGuid).Locate(designUid)));
            }
            throw new ArgumentException($"{nameof(AddDesignToSiteModel)} Unsupported ImportedFileType: {importedFileType}");
        }