Exemple #1
0
        public IHttpActionResult Get5([FromUri] Guid scenarioId,
                                      [FromUri] string fileId)
        {
            // Check output file is valid
            if (!fileId.Contains("."))      // Stupid hack, route doesn't work if contains full stop
            {
                fileId += ".out";
            }
            OutputFile outputFile = _outputFileRepository.Find(fileId);

            if (outputFile == null)
            {
                return(NotFound());
            }

            // Get temp data folder
            string tempFolder        = System.Web.Hosting.HostingEnvironment.MapPath("/Temp");
            string outputFilesFolder = Path.Combine(tempFolder, "OutputFiles");

            LocalOutputFiles localOutputFiles = new LocalOutputFiles(_resultsFileRepository, outputFilesFolder);

            try
            {
                // Get output files in compressed format
                localOutputFiles.GetOutputFiles(scenarioId, true, new List <OutputFile>()
                {
                    outputFile
                });

                // Returns file
                string outputFilePath      = localOutputFiles.GetOutputFilePath(scenarioId, true, outputFile);
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(File.ReadAllBytes(outputFilePath))
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                return(new System.Web.Http.Results.ResponseMessageResult(result));
            }
            finally
            {
                // Delete downloaded output files
                localOutputFiles.DeleteOutputFiles(scenarioId);
            }
        }
Exemple #2
0
        public IHttpActionResult Post([FromUri] Guid scenarioId,
                                      [FromBody] OutputFileSQLFilterModel command)
        {
            if (command == null || !ModelState.IsValid)
            {
                return(this.Error().InvalidParameters("Invalid parameters"));
            }

            ScenarioResult scenarioResult = _scenarioResultRepository.Find(scenarioId);

            if (scenarioResult == null)
            {
                return(NotFound());
            }

            if (String.IsNullOrEmpty(command.SQL))
            {
                return(this.Error().InvalidParameters("SQL is invalid"));
            }

            // Get temp data folder
            string tempFolder        = System.Web.Hosting.HostingEnvironment.MapPath("/Temp");
            string outputFilesFolder = string.Format(@"{0}\OutputFiles", tempFolder);

            SQLOutputFileFilterSettings filterSettings = null;

            string compressedResultsFile = "";

            LocalOutputFiles localOutputFiles = new LocalOutputFiles(_resultsFileRepository, outputFilesFolder);

            try
            {
                // Set filter settings,
                filterSettings = new SQLOutputFileFilterSettings()
                {
                    OutputFileFolder = string.Format(@"{0}\{1}\Data", outputFilesFolder, scenarioId),                       // Same folder that LocalOutputFileStorage stores files
                    SQL         = command.SQL,                                                                              // "select * from lmkii_spot_reqm.txt",
                    ResultsFile = string.Format(@"{0}\{1}\Results-{2}.txt", outputFilesFolder, scenarioId, Guid.NewGuid()), // Make unique in case two instances are querying
                    Delimiter   = ','
                };

                // Download all output files referenced by query
                localOutputFiles.GetOutputFiles(scenarioId, false, GetOutputFilesForSQL(filterSettings.SQL));

                // Apply filter
                SQLOutputFileFilter filter = new SQLOutputFileFilter();
                filter.Filter(filterSettings);

                /*
                 * var result = new HttpResponseMessage(HttpStatusCode.Created)
                 * {
                 *  Content = new System.Net.Http.ByteArrayContent(System.IO.File.ReadAllBytes(filterSettings.OutputFile)),
                 * };
                 */
                // Return results

                /*
                 * System.IO.Stream compressedStream = CompressionUtilities.CompressGZip(System.IO.File.OpenRead(filterSettings.OutputFile));
                 * var result = new HttpResponseMessage(HttpStatusCode.Created)
                 * {
                 *  Content = new System.Net.Http.StreamContent(compressedStream)
                 * };
                 *
                 * result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                 * result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                 * {
                 *  FileName = string.Format("Results-{0}.zip", id),
                 * };
                 */

                // Return file
                compressedResultsFile = System.IO.Path.Combine(outputFilesFolder, string.Format("{0}.zip", Guid.NewGuid()));
                CompressionUtilities.CompressGZipToFile(filterSettings.ResultsFile, compressedResultsFile);
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(System.IO.File.ReadAllBytes(compressedResultsFile))
                };
                result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                return(new System.Web.Http.Results.ResponseMessageResult(result));
            }
            finally
            {
                // Clean up
                localOutputFiles.DeleteOutputFiles(scenarioId);

                // Delete uncompressed results file
                if (!String.IsNullOrEmpty(filterSettings?.ResultsFile) && System.IO.File.Exists(filterSettings.ResultsFile))
                {
                    System.IO.File.Delete(filterSettings.ResultsFile);
                }

                // Delete compressed results file
                if (!String.IsNullOrEmpty(compressedResultsFile) && System.IO.File.Exists(compressedResultsFile))
                {
                    System.IO.File.Delete(compressedResultsFile);
                }
            }
        }