2012-03-13 3 views
2

파일을 다운로드하지 않고 각 메일 첨부 파일의 크기를 검색하고 싶습니다. Gmail gem을 사용하고 있습니다.GMAIL에서 IMAP을 사용하여 첨부 파일 크기 가져 오기

편집 : 매우 긴 프로세스 인 전체 첨부 파일을 검색하지 않고 헤더 나 유사한 것을 읽는 것만으로 크기를 얻는 방법을 찾고 있습니다.

gmail = Gmail.connect(:xoauth, self.email, 
    :token   => self.token, 
    :secret   => self.secret, 
    :consumer_key => 'SECRET', 
    :consumer_secret => 'SECRET' 
) 
mails = gmail.mailbox("[Gmail]/All Mail").emails 
mails.each do |mail| 
    next if mail.message.attachments.blank? 
    # How to get the message's size if possible ? 
    mail.message.attachments.each do |attachment| 
    # How to get the attachment's size ? 
    end 
end 

답변

1

다음은 내가 한 것입니다. StringIO를 사용하여 첨부 파일을 메모리에있는 파일로 변환 한 다음 크기를 가져 왔습니다.

require 'rubygems' 
require 'gmail' 
require 'ap' #awesome_print gem 

gmail = Gmail.connect("name","password") #simple authorization 
mails = gmail.mailbox("[Gmail]/All Mail").emails 
mails.each do |mail| 
    next if mail.message.attachments.blank? 
    mail.message.attachments.each do |attachment| 
    file = StringIO.new(attachment.to_s) 
    ap file.size 
    end 
end 
+0

답장을 보내 주셔서 감사합니다. 그러나이 방법은 너무 느립니다. 즉, 첨부 파일을 다운로드하지 않고 첨부 파일을 알 수있는 방법이 있습니까? 헤더 나 다른 것을 가져 오는 것만으로도? – Arkan

+0

나는 말할 수 없다 ... 크기에 대한 attr도없고 Mail :: Part 클래스에 대한 메서드도 없다. 적어도 전체 다운로드를 중지하고 객체를 메모리로 가져옵니다. – ScottJShea

+0

답장을 보내 주셔서 감사합니다. 아무도 대답하지 않으면 2 ~ 3 일 안에 답을 확인할 것입니다 :-) – Arkan

관련 문제