Menu

improvement on parsing the attachment

2004-02-24
2004-02-24
  • Wilson Chan

    Wilson Chan - 2004-02-24

    I have found there was some thing to be improved with the attachment class:

            private void parseHeader(StringReader sr,string temp)
            {
                string []array=Utility.getHeadersValue(temp);
                string []values=array[1].Split(";".ToCharArray());

                switch(array[0].ToUpper())
                {               
                    case "CONTENT-TYPE":
                        //_contentType=Utility.splitOnSemiColon(array[1])[0].Trim();
                        //if(array[1].ToLower().IndexOf("charset".ToLower())!=-1)
                        //    _contentCharset=Utility.splitOnSemiColon(array[1])[1].Trim();
                        //break;
                        if(values.Length>0)
                            _contentType=values[0].Trim();
                        if(values.Length>1)
                        {
                            _contentCharset=Utility.GetQuotedValue(values[1],"=","charset");
                        }
                        if(values.Length>2)
                        {
                            _contentFormat=Utility.GetQuotedValue(values[2],"=","format");
                        }
                        break;
                    case "CONTENT-TRANSFER-ENCODING":
                        _contentTransferEncoding=Utility.splitOnSemiColon(array[1])[0].Trim();
                        break;
                    case "CONTENT-DESCRIPTION":
                        _contentDescription=Utility.deCode(Utility.splitOnSemiColon(array[1])[0].Trim());
                        break;
                    case "CONTENT-DISPOSITION":
                        //_contentDisposition=Utility.splitOnSemiColon(array[1])[0].Trim();
    //                    _contentFileName=Utility.splitOnSemiColon(array[1])[1].Trim();
    //                    _contentFileName=_contentFileName.Substring(10,_contentFileName.Length-11);
                        if(values.Length>0)
                            _contentDisposition=values[0].Trim();
                       
                        _contentFileName=values[1];
                       
                        if(_contentFileName=="")
                            _contentFileName=sr.ReadLine();

                        _contentFileName=Utility.GetQuotedValue(_contentFileName,"=","filename");
                        _contentFileName=Utility.deCode(_contentFileName);
                        break;
                    case "Content-Id":
                        _contentID=Utility.splitOnSemiColon(array[1])[0].Trim();
                        break;
                }
            }

     
    • Wilson Chan

      Wilson Chan - 2004-02-24

              public bool IsEncoding(string encoding)
              {
                  return _contentTransferEncoding.ToLower().IndexOf(encoding.ToLower())!=-1;
              }

              /// <summary>
              /// Decode the attachment to text
              /// </summary>
              /// <returns>Decoded attachment text</returns>
              public string DecodeAttachmentAsText()
              {
                  string decodedAttachment=null;

                  if(_contentType=="message/rfc822")
                      decodedAttachment=Utility.deCode(_rawAttachment);
                  else if(_contentTransferEncoding!=null)
                  {
                      decodedAttachment=_rawAttachment;

                      if(!IsEncoding("7bit"))
                      {
                          if(IsEncoding("8bit")&&_contentCharset!=null&_contentCharset!="")
                              decodedAttachment=Utility.Change(decodedAttachment,_contentCharset);

                          decodedAttachment=Utility.deCodeB64s(Utility.RemoveNonB64(decodedAttachment));
                          //decodedAttachment=Encoding.Default.GetString(Convert.FromBase64String(Utility.RemoveNonB64(decodedAttachment)));
                      }
                  }
                  else if(_contentCharset!=null)
                      decodedAttachment=Utility.Change(_rawAttachment,_contentCharset);//Encoding.Default.GetString(Encoding.GetEncoding(_contentCharset).GetBytes(_rawAttachment));
                  else
                      decodedAttachment=_rawAttachment;

                  return decodedAttachment;
              }

              public Message DecodeAsMessage()
              {
                  return new Message(_rawAttachment,false);
              }

              /// <summary>
              /// Decode the attachment to bytes
              /// </summary>
              /// <returns>Decoded attachment bytes</returns>
              public byte[] DecodedAttachmentAsBytes()
              {
                  if(_rawAttachment==null)
                      return null;
                  if(_contentFileName!="")
                      //return Convert.FromBase64String(Utility.RemoveNonB64(_rawAttachment));
                      //return Encoding.Default.GetBytes(DecodeAttachment());
                  {
                      byte []decodedBytes=null;

                      if(_contentType=="message/rfc822")
                          decodedBytes=Encoding.Default.GetBytes(Utility.deCode(_rawAttachment));
                      else if(_contentTransferEncoding!=null)
                      {
                          if(!IsEncoding("7bit"))
                          {
                              string content=_rawAttachment;
                              if(IsEncoding("8bit")&&_contentCharset!=null&_contentCharset!="")
                                  content=Utility.Change(content,_contentCharset);

                              decodedBytes=Convert.FromBase64String(Utility.RemoveNonB64(content));
                          }
                      }
                      else if(_contentCharset!=null)
                          decodedBytes=Encoding.Default.GetBytes(Utility.Change(_rawAttachment,_contentCharset));//Encoding.Default.GetString(Encoding.GetEncoding(_contentCharset).GetBytes(_rawAttachment));
                      else
                          decodedBytes=Encoding.Default.GetBytes(_rawAttachment);

                      return decodedBytes;
                  }
                  else
                  {
                      return null;
                  }
              }

       
    • Wilson Chan

      Wilson Chan - 2004-02-24

              public static string GetQuotedValue(string text, string splitter, string tag)
              {
                  if(text==null)
                      throw new ArgumentNullException("text","Argument was null");

                  string []array=new String[2]{"",""};
                  int indexOfSplitter=text.IndexOf(splitter);           

                  try
                  {
                      array[0]=text.Substring(0,indexOfSplitter).Trim();
                      array[1]=text.Substring(indexOfSplitter+1).Trim();
                      int pos=array[1].IndexOf("\&quot;");
                      if(pos!=-1)
                      {
                          int pos2=array[1].IndexOf("\&quot;",pos+1);
                          array[1]=array[1].Substring(pos+1,pos2-pos-1);
                      }
                  }
                  catch(Exception){}

                  //return array;
                  if(array[0].ToLower()==tag.ToLower())
                      return array[1].Trim();
                  else
                      return null;
              }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.