Ejemplo n.º 1
0
        public IActionResult RetrieveMostRecentCSVFile()
        {
            ICSVFileStream csvfile = _csvfileProvider.GetMostRecentCSVFile();

            if (csvfile == null)
            {
                return(BadRequest(new ErrorResponse("No such csv file")));
            }

            // We need to copy the csvfile to an internal memory stream in order
            // to transfer it out with a byte array. This is probably inefficient
            // since we copy once to the filesystem to memory and then from
            // memory to HTTP - worth refactoring later
            var dataStream = new MemoryStream();

            csvfile.CopyTo(dataStream);
            return(File(dataStream.ToArray(), csvfile.Format.ContentTypeString()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// List the TimeStamps of all the stored csv files in the database
        /// </summary>

        /* public IReadOnlyList<long> StoredCSVFiles
         * {
         *   get
         *   {
         *       using (var dbContext = new AllocationContext(_dbOptions))
         *       {
         *           return dbContext.CSVFileStorage.Select(c => c.TimeStamp).ToList();
         *       }
         *   }
         * }*/

        /// <summary>
        /// Store a csv file in the database
        /// </summary>
        /// <param name="csvfileData"> the csvfile data stream to store csvfile data from</param>
        /// <returns>the database TimeStamp of the newly stored file</returns>
        public CSVFileStorage CreateCSVFile(ICSVFileStream csvfileData)
        {
            //SKData cv = EncodeFile(csvfileData);
            if (csvfileData == null)
            {
                throw new ArgumentException("Invalid file or unsupported format");
            }

            //byte[] fileBytes = new byte[csvfileData.Length];
            //FileStream filestream = csvfileData as FileStream;
            //filestream.Read(fileBytes, 0, fileBytes.Length);
            //csvfileData.

            byte[] fileBytes = new byte[csvfileData.Length];

            if (csvfileData.Length > 0)
            {
                using (var ms = new MemoryStream())
                {
                    csvfileData.File.CopyTo(ms);
                    fileBytes = ms.ToArray();
                }
            }

            var csvfilestorage = new CSVFileStorage()
            {
                CSVFileData = fileBytes, CSVTimeStamp = csvfileData.TimeStamp, CSVFileFormat = csvfileData.Format
            };

            using (var dbContext = new AllocationContext(_dbOptions))
            {
                dbContext.CSVFileStorages.Add(csvfilestorage);
                dbContext.SaveChanges();

                return(csvfilestorage);

                //return csvfileData.TimeStamp;
            }
        }
Ejemplo n.º 3
0
        public IActionResult UploadCSVFile(IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest(new ErrorResponse("Bad request format.")));
            }

            ICSVFileStream csvfile = HttpCSVFileStream.CreateFromIFormFile(file);

            if (csvfile == null)
            {
                return(BadRequest(new ErrorResponse("Invalid CSVFile or unsupported format")));
            }

            try
            {
                CSVFileStorage savedCSV = _csvfileProvider.CreateCSVFile(csvfile);
                return(Json(new CSVFileIdResponse(savedCSV.CSVFileID)));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new ErrorResponse(ex.Message)));
            }
        }