public static List <FileProcessInfo> GetActiveProcesses() { using (var db = new FileUploadDbEntities()) { var completedState = (byte)ProcessState.Completed; var processes = db.EFFileProcessInfoes .Include("DataFileInfo") .Where(p => p.State != completedState).ToList(); List <FileProcessInfo> result = new List <FileProcessInfo>(); foreach (var process in processes) { var processInfo = new FileProcessInfo(); processInfo.Id = process.Id; processInfo.Message = process.Message; processInfo.UniqueId = process.UniqueId; processInfo.StartTime = process.StartTime; processInfo.State = (ProcessState)process.State; processInfo.Result = (ProcessResult)process.Result; processInfo.FileInfo = new DataFileInfo(); processInfo.FileInfo.Name = process.DataFileInfo.Name; processInfo.FileInfo.Hash = process.DataFileInfo.Hash; processInfo.FileInfo.Size = process.DataFileInfo.Size; processInfo.FileInfo.CreationTime = process.DataFileInfo.CreationTime; result.Add(processInfo); } return(result); } }
public static bool FileExists(DataFileInfo fileInfo, out FileProcessInfo inProcess) { if (fileInfo == null) { throw new ArgumentNullException(nameof(fileInfo)); } bool result = false; inProcess = null; using (var db = new FileUploadDbEntities()) { var existingFile = db.EFDataFileInfoes .FirstOrDefault(f => f.Hash == fileInfo.Hash); if (existingFile != null) { result = true; if (!existingFile.FileProcessInfoes.IsLoaded) { existingFile.FileProcessInfoes.Load(); } var processInfo = existingFile.FileProcessInfoes.FirstOrDefault(); if (processInfo != null) { inProcess = new FileProcessInfo(); inProcess.Id = processInfo.Id; inProcess.FileInfo = new DataFileInfo() { CreationTime = existingFile.CreationTime, Hash = existingFile.Hash, Name = existingFile.Name, Size = existingFile.Size, }; inProcess.Result = (ProcessResult)processInfo.Result; inProcess.StartTime = processInfo.StartTime; inProcess.State = (ProcessState)processInfo.State; } } } return(result); }
public bool insert(IEnumerable <FileUpload> fileuploads) { bool result = false; try { using (var ctx = new FileUploadDbEntities()) { ctx.FileUpload.AddRange(fileuploads); ctx.SaveChanges(); result = true; } } catch (Exception ex) { throw new Exception(ex.Message); } return(result); }
public IEnumerable <FileUploadModel> getAll() { IEnumerable <FileUploadModel> result = null; try { using (var ctx = new FileUploadDbEntities()) { result = (from f in ctx.FileUpload select new FileUploadModel { Id = f.TransactionId, Payment = f.Amount.ToString() + " " + f.CurrencyCode, Status = ((f.Status == "Finished" || f.Status == "Done") ? "D" : ((f.Status == "Failed" || f.Status == "Rejected") ? "R" : "A")) }).ToList(); } } catch (Exception ex) { throw new Exception(ex.Message); } return(result); }
public static FileProcessInfo StartProcess(FileProcessInfo startInfo, Stream dataStream) { if (startInfo == null) { throw new ArgumentNullException(nameof(startInfo)); } if (dataStream == null) { throw new ArgumentNullException(nameof(dataStream)); } //todo dir name var dirName = Environment.ExpandEnvironmentVariables("%temp%\\FileUploadManager"); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } var tempFileName = Path.Combine(dirName, startInfo.UniqueId.ToString()); try { using (var fs = new FileStream(tempFileName, FileMode.Create, FileAccess.Write)) { var buffer = new byte[64 * 1024]; int readCount; while ((readCount = dataStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, readCount); } } using (var db = new FileUploadDbEntities()) { db.Connection.Open(); using (var tran = db.Connection.BeginTransaction()) { var fileInfo = new EFDataFileInfo(); fileInfo.CreationTime = startInfo.FileInfo.CreationTime; fileInfo.Hash = startInfo.FileInfo.Hash; fileInfo.HostName = Environment.MachineName; fileInfo.Name = startInfo.FileInfo.Name; fileInfo.Path = tempFileName; fileInfo.Size = startInfo.FileInfo.Size; //todo: GETDATE() fileInfo.InsertTime = DateTime.Now; EFFileProcessInfo processInfo = new EFFileProcessInfo(); processInfo.DataFileInfo = fileInfo; processInfo.StartTime = startInfo.StartTime; processInfo.UniqueId = startInfo.UniqueId; processInfo.State = (byte)ProcessState.Parsing; db.AddToEFFileProcessInfoes(processInfo); db.SaveChanges(); tran.Commit(); startInfo.Id = processInfo.Id; startInfo.State = (ProcessState)processInfo.State; return(startInfo); } } } catch (Exception e) { try { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } catch (Exception innerException) { //todo use FT.Common e.Data["InnerException"] = innerException.ToString(); } throw; } }