} // proc Dispose public async Task <IOdetteFileWriter> CreateInFileAsync(IOdetteFile file, string userData) { var incomingFile = String.Format("In coming file {0} ", OdetteFileImmutable.FormatFileName(file, userData)); if (!service.IsInFileAllowed(file)) { log.Info(incomingFile + "ignored"); return(null); } var fi = service.CreateInFileName(file, OdetteInFileState.Pending); // check if the file exists if (File.Exists(Path.ChangeExtension(fi.FullName, GetInFileExtention(OdetteInFileState.Received))) || File.Exists(Path.ChangeExtension(fi.FullName, GetInFileExtention(OdetteInFileState.PendingEndToEnd))) || File.Exists(Path.ChangeExtension(fi.FullName, GetInFileExtention(OdetteInFileState.Finished)))) { throw new OdetteFileServiceException(OdetteAnswerReason.DuplicateFile, "File already exists.", false); } // open the file to write var fileItem = await Task.Run(() => new FileItem(service, fi, file, true)); fileItem.Log(log, incomingFile + "accepted"); try { return(await Task.Run(new Func <IOdetteFileWriter>(fileItem.OpenWrite))); } catch (IOException e) { throw new OdetteFileServiceException(OdetteAnswerReason.UnspecifiedReason, e.Message, false, e); } } // func CreateInFile
} // func IsInFileAllowed /// <summary>Creates the file name, for the description, without state information.</summary> /// <param name="fileDescription"></param> /// <returns></returns> private static string GetFileName(IOdetteFile fileDescription) { if (fileDescription == null) { throw new ArgumentNullException("fileDescription"); } if (String.IsNullOrEmpty(fileDescription.VirtualFileName)) { throw new ArgumentNullException("VirtualFileName"); } if (fileDescription.FileStamp == null) { throw new ArgumentNullException("FileStamp"); } return(fileDescription.SourceOrDestination + "#" + fileDescription.VirtualFileName + "#" + fileDescription.FileStamp.ToString(fileStampFormat, CultureInfo.InvariantCulture)); } // func GetFileName
} // proc CallFileItemNotify #endregion #region -- Directory Helper --------------------------------------------------- /// <summary>Checks if the file is receivable.</summary> /// <param name="fileDescription"></param> /// <returns></returns> internal bool IsInFileAllowed(IOdetteFile fileDescription) { if (directoryIn == null) { return(false); } if (fileNameFilter == null || fileNameFilter.Length == 0) { return(true); } foreach (var cur in fileNameFilter) { if (Procs.IsFilterEqual(fileDescription.VirtualFileName, cur)) { return(true); } } return(false); } // func IsInFileAllowed
/// <summary></summary> /// <param name="file"></param> /// <param name="userData"></param> /// <returns></returns> public static string FormatFileName(IOdetteFile file, string userData) => file.SourceOrDestination + "/" + file.VirtualFileName + "[userData=" + userData + "]";
public FileItem(DirectoryFileServiceItem notifyTarget, FileInfo fileInfo, IOdetteFile file, bool createInfo) { var fileDescription = file as IOdetteFileDescription; this.notifyTarget = new WeakReference <DirectoryFileServiceItem>(notifyTarget); this.fileInfo = fileInfo; this.virtualFileName = file.VirtualFileName; this.fileStamp = file.FileStamp; this.sourceOrDestination = file.SourceOrDestination; var fiExtended = new FileInfo(GetExtendedFile()); var readed = false; if (!createInfo && fiExtended.Exists) { // read file xExtentions = XDocument.Load(fiExtended.FullName); // read description element var xDescription = xExtentions.Root.Element("description") ?? new XElement("description"); // get the attributes var stringFormat = xDescription.GetAttribute("format", "U"); if (string.IsNullOrEmpty(stringFormat)) { format = OdetteFileFormat.Unstructured; } else { switch (Char.ToUpper(stringFormat[0])) { case 'T': format = OdetteFileFormat.Text; break; case 'F': format = OdetteFileFormat.Fixed; break; case 'V': format = OdetteFileFormat.Variable; break; default: format = OdetteFileFormat.Unstructured; break; } } maximumRecordSize = xDescription.GetAttribute("maximumRecordSize", 0); fileSize = xDescription.GetAttribute("fileSize", -1L); if (fileSize < 0) { fileSize = fileInfo.Length / 1024; // 1ks if ((fileInfo.Length & 0x3FF) != 0) { fileSize++; } } fileSizeUnpacked = xDescription.GetAttribute("fileSizeUnpacked", fileSize); description = xDescription.Value ?? String.Empty; readed = true; } if (!readed) // create the new info xml { this.format = fileDescription?.Format ?? OdetteFileFormat.Unstructured; this.maximumRecordSize = fileDescription?.MaximumRecordSize ?? 0; this.fileSize = fileDescription?.FileSize ?? (fileInfo.Exists ? fileInfo.Length : 0); this.fileSizeUnpacked = fileDescription?.FileSizeUnpacked ?? fileSize; this.description = fileDescription?.Description ?? String.Empty; // create extented attributes xExtentions = new XDocument( new XDeclaration("1.0", Encoding.Default.WebName, "yes"), new XElement("oftp", new XElement("description", new XAttribute("format", format), new XAttribute("maximumRecordSize", maximumRecordSize), new XAttribute("fileSize", fileSize), new XAttribute("fileSizeUnpacked", fileSizeUnpacked), new XText(description) ) ) ); if (!fiExtended.Exists) { xExtentions.Save(fiExtended.FullName); } } // optional information for send xSendInfo = xExtentions.Root.Element("send") ?? new XElement("send"); } // ctor
/// <summary>Builds the file name with state extention.</summary> /// <param name="file"></param> /// <param name="state"></param> /// <returns></returns> internal FileInfo CreateOutFileName(IOdetteFile file, OdetteOutFileState state) => new FileInfo(Path.Combine(directoryOut.FullName, GetFileName(file) + GetOutFileExtention(state)));
} // proc CallFileItemNotify #endregion #region -- Directory Helper ------------------------------------------------------- /// <summary>Checks if the file is receivable.</summary> /// <param name="fileDescription"></param> /// <returns></returns> internal bool IsInFileAllowed(IOdetteFile fileDescription) { if (directoryIn == null) return false; if (fileNameFilter == null || fileNameFilter.Length == 0) return true; foreach (var cur in fileNameFilter) { if (ProcsDE.IsFilterEqual(fileDescription.VirtualFileName, cur)) return true; } return false; } // func IsInFileAllowed
} // func IsInFileAllowed /// <summary>Creates the file name, for the description, without state information.</summary> /// <param name="fileDescription"></param> /// <returns></returns> private static string GetFileName(IOdetteFile fileDescription) { if (fileDescription == null) throw new ArgumentNullException("fileDescription"); if (String.IsNullOrEmpty(fileDescription.VirtualFileName)) throw new ArgumentNullException("VirtualFileName"); if (fileDescription.FileStamp == null) throw new ArgumentNullException("FileStamp"); return fileDescription.Originator + "#" + fileDescription.VirtualFileName + "#" + fileDescription.FileStamp.ToString(FileStampFormat, CultureInfo.InvariantCulture); } // func GetFileName
} // proc Dispose public IOdetteFileWriter CreateInFile(IOdetteFile file, string userData) { var incomingFile = String.Format("In coming file {0} ", OdetteFileMutable.FormatFileName(file, userData)); if (!service.IsInFileAllowed(file)) { log.Info(incomingFile + "ignored"); return null; } var fi = service.CreateInFileName(file, OdetteInFileState.Pending); // check if the file exists if (File.Exists(Path.ChangeExtension(fi.FullName, GetInFileExtention(OdetteInFileState.Received))) || File.Exists(Path.ChangeExtension(fi.FullName, GetInFileExtention(OdetteInFileState.PendingEndToEnd))) || File.Exists(Path.ChangeExtension(fi.FullName, GetInFileExtention(OdetteInFileState.Finished)))) throw new OdetteFileServiceException(OdetteAnswerReason.DuplicateFile, "File already exists.", false); // open the file to write var fileItem = new FileItem(service, fi, file, true); fileItem.Log(log, incomingFile + "accepted"); try { return fileItem.OpenWrite(); } catch (IOException e) { throw new OdetteFileServiceException(OdetteAnswerReason.UnspecifiedReason, e.Message, false, e); } } // func CreateInFile
public FileItem(DirectoryFileServiceItem notifyTarget, FileInfo fileInfo, IOdetteFile file, bool createInfo) { var fileDescription = file as IOdetteFileDescription; this.notifyTarget = new WeakReference<DirectoryFileServiceItem>(notifyTarget); this.fileInfo = fileInfo; this.virtualFileName = file.VirtualFileName; this.fileStamp = file.FileStamp; this.originator = file.Originator; var fiExtended = new FileInfo(GetExtendedFile()); var readed = false; if (!createInfo && fiExtended.Exists) { // read file xExtentions = XDocument.Load(fiExtended.FullName); // read description element var xDescription = xExtentions.Root.Element("description") ?? new XElement("description"); // get the attributes var stringFormat = xDescription.GetAttribute("format", "U"); if (string.IsNullOrEmpty(stringFormat)) format = OdetteFileFormat.Unstructured; else { switch (Char.ToUpper(stringFormat[0])) { case 'T': format = OdetteFileFormat.Text; break; case 'F': format = OdetteFileFormat.Fixed; break; case 'V': format = OdetteFileFormat.Variable; break; default: format = OdetteFileFormat.Unstructured; break; } } maximumRecordSize = xDescription.GetAttribute("maximumRecordSize", 0); fileSize = xDescription.GetAttribute("fileSize", -1L); if (fileSize < 0) { fileSize = fileInfo.Length / 1024; // 1ks if ((fileInfo.Length & 0x3FF) != 0) fileSize++; } fileSizeUnpacked = xDescription.GetAttribute("fileSizeUnpacked", fileSize); description = xDescription.Value ?? String.Empty; readed = true; } if (!readed) // create the new info xml { this.format = fileDescription?.Format ?? OdetteFileFormat.Unstructured; this.maximumRecordSize = fileDescription?.MaximumRecordSize ?? 0; this.fileSize = fileDescription?.FileSize ?? (fileInfo.Exists ? fileInfo.Length : 0); this.fileSizeUnpacked = fileDescription?.FileSizeUnpacked ?? fileSize; this.description = fileDescription?.Description ?? String.Empty; // create extented attributes xExtentions = new XDocument( new XDeclaration("1.0", Encoding.Default.WebName, "yes"), new XElement("oftp", new XElement("description", new XAttribute("format", format), new XAttribute("maximumRecordSize", maximumRecordSize), new XAttribute("fileSize", fileSize), new XAttribute("fileSizeUnpacked", fileSizeUnpacked), new XText(description) ) ) ); if (!fiExtended.Exists) xExtentions.Save(fiExtended.FullName); } // optional information for send xSendInfo = xExtentions.Root.Element("send") ?? new XElement("send"); } // ctor
} // func CreateInFileName /// <summary>Builds the file name with state extention.</summary> /// <param name="file"></param> /// <param name="state"></param> /// <returns></returns> internal FileInfo CreateOutFileName(IOdetteFile file, OdetteOutFileState state) { return new FileInfo(Path.Combine(directoryOut.FullName, GetFileName(file) + GetOutFileExtention(state))); } // func CreateOutFileName
} // proc Dispose #endregion #region -- File service proxy implementation -------------------------------------- /// <summary>Create/Overrides/Resumes a new odette file, to receive.</summary> /// <param name="fileDescription"></param> /// <param name="userData"></param> /// <returns></returns> public IOdetteFileWriter CreateInFile(IOdetteFile file, string userData) { foreach (var s in services) { var inFile = s.CreateInFile(file, userData); if (inFile != null) return inFile; } return null; } // func CreateInFile
public static string FormatFileName(IOdetteFile file, string userData) => file.Originator + "/" + file.VirtualFileName + "[userData=" + userData + "]";