internal bool ParseMixedMessage(string buffer, string boundary) { int start=0,end; for(;;) { start=buffer.IndexOf(boundary,start); if (start==-1) break; end=buffer.IndexOf(boundary,start+1); if (end==-1) break; // remove boundary from buffer start += boundary.Length; Attachment a=new Attachment( buffer.Substring(start,end-start-2) ); this.attachments.Add(a); } return true; }
/// <summary> /// Stores an attachment to disk. /// </summary> /// <param name="attachment"></param> /// <param name="binariesPath"></param> /// <returns></returns> public string StoreAttachment(Attachment attachment, string binariesPath) { bool alreadyUploaded = false; string baseFileName = attachment.fileName; string targetFileName = Path.Combine(binariesPath, baseFileName); int numSuffix = 1; // if the target filename already exists, we check whether we already // have that file stored by comparing the first 2048 bytes of the incoming // date to the target file (creating a hash would be better, but this is // "good enough" for the time being) while (File.Exists(targetFileName)) { byte[] targetBuffer = new byte[Math.Min(2048, attachment.data.Length)]; int targetBytesRead; using (FileStream targetFile = new FileStream(targetFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { long numBytes = targetFile.Length; if (numBytes == (long)attachment.data.Length) { targetBytesRead = targetFile.Read(targetBuffer, 0, targetBuffer.Length); if (targetBytesRead == targetBuffer.Length) { if (EqualBuffers(targetBuffer, attachment.data, targetBuffer.Length)) { alreadyUploaded = true; } } } } // If the file names are equal, but it's not considered the same file, // we append an incrementing numeric suffix to the file name and retry. if (!alreadyUploaded) { string ext = Path.GetExtension(baseFileName); string file = Path.GetFileNameWithoutExtension(baseFileName); string newFileName = file + (numSuffix++).ToString(); baseFileName = newFileName + ext; targetFileName = Path.Combine(binariesPath, baseFileName); } else { break; } } // now we've got a unique file name or the file is already stored. If it's // not stored, write it to disk. if (!alreadyUploaded) { using (FileStream fileStream = new FileStream(targetFileName, FileMode.Create)) { fileStream.Write(attachment.data, 0, attachment.data.Length); fileStream.Flush(); } } return(baseFileName); }
/// <summary> /// Stores an attachment to disk. /// </summary> /// <param name="attachment"></param> /// <param name="binariesPath"></param> /// <returns></returns> public string StoreAttachment( Attachment attachment, string binariesPath ) { bool alreadyUploaded = false; string baseFileName = attachment.fileName; string targetFileName = Path.Combine(binariesPath,baseFileName); int numSuffix=1; // if the target filename already exists, we check whether we already // have that file stored by comparing the first 2048 bytes of the incoming // date to the target file (creating a hash would be better, but this is // "good enough" for the time being) while ( File.Exists(targetFileName)) { byte[] targetBuffer=new byte[Math.Min(2048,attachment.data.Length)]; int targetBytesRead; using(FileStream targetFile = new FileStream(targetFileName,FileMode.Open,FileAccess.Read,FileShare.Read)) { long numBytes = targetFile.Length; if ( numBytes == (long)attachment.data.Length ) { targetBytesRead = targetFile.Read(targetBuffer,0,targetBuffer.Length); if ( targetBytesRead == targetBuffer.Length ) { if ( EqualBuffers(targetBuffer, attachment.data, targetBuffer.Length ) ) { alreadyUploaded=true; } } } } // If the file names are equal, but it's not considered the same file, // we append an incrementing numeric suffix to the file name and retry. if ( !alreadyUploaded ) { string ext = Path.GetExtension(baseFileName); string file = Path.GetFileNameWithoutExtension(baseFileName); string newFileName = file + (numSuffix++).ToString(); baseFileName = newFileName+ext; targetFileName = Path.Combine(binariesPath,baseFileName); } else { break; } } // now we've got a unique file name or the file is already stored. If it's // not stored, write it to disk. if ( !alreadyUploaded ) { using(FileStream fileStream = new FileStream(targetFileName,FileMode.Create)) { fileStream.Write(attachment.data,0,attachment.data.Length); fileStream.Flush(); } } return baseFileName; }