Esempio n. 1
0
        /// <summary>
        /// Gets file to return in HTTP response
        /// </summary>
        /// <param name="file"></param>
        /// <param name="compressionFormat"></param>
        /// <param name="attachment"></param>
        /// <returns></returns>
        private static ResponseMessageResult GetFile(string file, string compressionFormat, bool attachment)
        {
            string logFolder = System.Web.Hosting.HostingEnvironment.MapPath("/Logs");

            if (!File.Exists(file))
            {
                return(null);
            }

            string compressedFile = "";

            try
            {
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                switch (compressionFormat)
                {
                case "gzip":
                    compressedFile = Path.Combine(logFolder, string.Format("{0}.zip", Guid.NewGuid().ToString()));
                    CompressionUtilities.CompressGZipToFile(file, compressedFile);
                    result.Content = new System.Net.Http.ByteArrayContent(System.IO.File.ReadAllBytes(compressedFile));
                    result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

                    // Return as attachment
                    if (attachment)
                    {
                        result.Content.Headers.Add("Content-Encoding", "gzip");
                    }

                    break;

                default:
                    result.Content = new System.Net.Http.ByteArrayContent(System.IO.File.ReadAllBytes(file));
                    result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

                    break;
                }

                if (attachment)
                {
                    result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                    {
                        FileName = Path.GetFileName(file)
                    };
                }

                return(new System.Web.Http.Results.ResponseMessageResult(result));
            }
            finally
            {
                // Clean up
                if (!String.IsNullOrEmpty(compressedFile) && File.Exists(compressedFile))
                {
                    File.Delete(compressedFile);
                }
            }
        }
Esempio n. 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);
                }
            }
        }