2017-03-17 108 views
1

抓取電子郵件時,我運行gmail.users.messages.get(),然後運行以下兩個函數來處理​​。node.js Gmail API:獲取嵌入式/嵌入式圖像

function getBody(message) { 
    var encodedBody = ''; 
    try{  
    if(typeof message.parts === 'undefined'){ 
     encodedBody = message.body.data; 
    }  
    else{ 
     encodedBody = getHTMLPart(message.parts); 
    } 
    encodedBody = encodedBody.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, ''); 
    } 
    catch(e) {} // there was a failure 

    return decodeURIComponent(escape(window.atob(encodedBody))); 
} 
function getHTMLPart(arr) { 

    for(var x = 0; x <= arr.length; x++){  
    if(typeof arr[x].parts === 'undefined'){ 
     if(arr[x].mimeType === 'text/html'){ 
     return arr[x].body.data; 
     } 
    } 
    else{  
     return getHTMLPart(arr[x].parts); 
    } 
    } 
    return ''; 
} 

然後我將這些數據保存到.html文件中供以後使用。問題在於內聯圖像沒有嵌入base64或其他方式來檢索數據,而是使用唯一的CID進行嵌入。

所以我需要做的是,從上面的功能,檢索有效載荷時,我還需要找回嵌入式圖像並在本地保存它作爲< \ CID.png>(或JPG或其他)。然後,我可以在消息上替換嵌入在HTML中的CID與圖像的本地路徑。

那麼有誰知道如何獲得這些嵌入式圖像或有任何建議?提前致謝!

回答

1

圖像將被提取到附件中。在響應中查找包含cid,Content-IDX-Attachment-Id標題的部分,獲取附件,然後插入base64數據作爲圖像源,而不是cid

var response = { 
 
"id": "15ade50437b9aa01", 
 
"threadId": "15ade50437b9aa01", 
 
"labelIds": [ 
 
    "UNREAD", 
 
    "IMPORTANT", 
 
    "SENT", 
 
    "INBOX" 
 
], 
 
"snippet": "", 
 
"historyId": "1171380", 
 
"internalDate": "1489788486000", 
 
"payload": { 
 
    "mimeType": "multipart/related", 
 
    "filename": "", 
 
    "headers": [ 
 
    { 
 
    "name": "Content-Type", 
 
    "value": "multipart/related; boundary=94eb2c034184892a95054af46913" 
 
    } 
 
    ], 
 
    "body": { 
 
    "size": 0 
 
    }, 
 
    "parts": [ 
 
    { 
 
    "mimeType": "multipart/alternative", 
 
    "filename": "", 
 
    "headers": [ 
 
    { 
 
     "name": "Content-Type", 
 
     "value": "multipart/alternative; boundary=94eb2c034184892a93054af46912" 
 
    } 
 
    ], 
 
    "body": { 
 
    "size": 0 
 
    }, 
 
    "parts": [ 
 
    { 
 
     "partId": "0.0", 
 
     "mimeType": "text/plain", 
 
     "filename": "", 
 
     "headers": [ 
 
     { 
 
     "name": "Content-Type", 
 
     "value": "text/plain; charset=UTF-8" 
 
     } 
 
     ], 
 
     "body": { 
 
     "size": 25, 
 
     "data": "W2ltYWdlOiBJbmZvZ2FkIGJpbGQgMV0NCg==" 
 
     } 
 
    }, 
 
    { 
 
     "partId": "0.1", 
 
     "mimeType": "text/html", 
 
     "filename": "", 
 
     "headers": [ 
 
     { 
 
     "name": "Content-Type", 
 
     "value": "text/html; charset=UTF-8" 
 
     } 
 
     ], 
 
     "body": { 
 
     "size": 106, 
 
     "data": "PGRpdiBkaXI9Imx0ciI-PGltZyBzcmM9ImNpZDppaV8xNWFkZTUwMmVlYTg0MGNlIiBhbHQ9IkluZm9nYWQgYmlsZCAxIiB3aWR0aD0iNTgiIGhlaWdodD0iNTQiPjxicj48L2Rpdj4NCg==" 
 
     } 
 
    } 
 
    ] 
 
    }, 
 
    { 
 
    "partId": "1", 
 
    "mimeType": "image/png", 
 
    "filename": "smile.png", 
 
    "headers": [ 
 
    { 
 
     "name": "Content-Type", 
 
     "value": "image/png; name=\"smile.png\"" 
 
    }, 
 
    { 
 
     "name": "Content-Disposition", 
 
     "value": "inline; filename=\"smile.png\"" 
 
    }, 
 
    { 
 
     "name": "Content-Transfer-Encoding", 
 
     "value": "base64" 
 
    }, 
 
    { 
 
     "name": "Content-ID", 
 
     "value": "\u003cii_15ade502eea840ce\u003e" 
 
    }, 
 
    { 
 
     "name": "X-Attachment-Id", 
 
     "value": "ii_15ade502eea840ce" 
 
    } 
 
    ], 
 
    "body": { 
 
    "attachmentId": "ANGjdJ8Xh1_0DBjFbc2qKRHD8uTw-9nkPP30v-vohJforDg54EHPHf3Obd2P9W6Wfss0cwfmblQWi5F3958vcEi0HyiMNgpKJbsQAVP9viUOY4LzyxwAvR7-dis4PNGflBpkZFMHv62LGKkQ1-ZPG3Go_Xh_sXJUveHl4JjmwLpNp6LjlHzuA_3XOkY2LLQLFmXNTo_dJbqDQWvMb8UTGnATMOoTNKvNQ4Ndr9pgQYI1SBvtdThgUDmlOGKYLHM6qR4AlrNNFnPUCZZU-BB7o7Dt2dhj-kexiIdvaB2LEnoeCBth_oK9HELt2tw4rlY", 
 
    "size": 8539 
 
    } 
 
    } 
 
    ] 
 
}, 
 
"sizeEstimate": 12800 
 
}; 
 

 
function getHtml(res) { 
 
    var parts = [res.payload]; 
 
    while (parts.length) { 
 
    var part = parts.shift(); 
 
    if (part.parts) { 
 
     parts = parts.concat(part.parts); 
 
    } 
 

 
    if(part.mimeType === 'text/html') { 
 
     return decodeURIComponent(escape(atob(part.body.data.replace(/\-/g, '+').replace(/\_/g, '/')))); 
 
    } 
 
    } 
 
    return ''; 
 
} 
 

 
function getAttachmentId(res, cid) { 
 
    var parts = [res.payload]; 
 
    while (parts.length) { 
 
    var part = parts.shift(); 
 
    if (part.parts) { 
 
     parts = parts.concat(part.parts); 
 
    } 
 
    var headers = part.headers; 
 
    var indexedHeaders = headers.reduce(function(acc, header) { 
 
     acc[header.name.toLowerCase()] = header.value; 
 
     return acc; 
 
    }, {}); 
 
    var contentId = indexedHeaders['content-id'] || ''; 
 
    var xAttachmentId = indexedHeaders['x-attachment-id'] || ''; 
 
    if (contentId.includes(cid) || xAttachmentId.includes(cid)) { 
 
     return part.body.attachmentId; 
 
    } 
 
    } 
 
    return ''; 
 
} 
 

 
var html = getHtml(response); 
 
console.log(html); 
 
// Extract the cids and find the matching attachments in the response 
 
var attachmentId = getAttachmentId(response, 'ii_15ade502eea840ce'); 
 
console.log(attachmentId); 
 
// Get the attachment from the Gmail API and replace the cid 
 
// with the base64-data

+1

謝謝!我已經成功提取了消息附件,但不知道內聯是否也作爲附件存儲,並且不知道如何將「cid」與文件名匹配。這個答案非常有幫助,謝謝! – Harmonic

+0

@Harmonic太棒了!樂於幫助。 – Tholle