浏览代码

Dont include blobs multiple time.

Mark Scherer 10 年之前
父节点
当前提交
4f19e3fa11
共有 1 个文件被更改,包括 23 次插入0 次删除
  1. 23 0
      Lib/EmailLib.php

+ 23 - 0
Lib/EmailLib.php

@@ -190,6 +190,10 @@ class EmailLib extends CakeEmail {
 			$ext = pathinfo($filename, PATHINFO_EXTENSION);
 			$mimeType = $this->_getMimeByExtension($ext);
 		}
+		if ($contentId === null && ($cid = $this->_isEmbeddedBlobAttachment($content, $filename))) {
+			return $cid;
+		}
+
 		$options['content'] = $content;
 		$options['mimetype'] = $mimeType;
 		$options['contentId'] = $contentId ? $contentId : str_replace('-', '', CakeText::uuid()) . '@' . $this->_domain;
@@ -221,6 +225,25 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
+	 * Returns if this particular file has already been attached as embedded file with this exact name
+	 * to prevent the same image to overwrite each other and also to only send this image once.
+	 * Allows multiple usage of the same embedded image (using the same cid)
+	 *
+	 * @return string cid of the found file or false if no such attachment can be found
+	 */
+	protected function _isEmbeddedBlobAttachment($content, $name) {
+		foreach ($this->_attachments as $filename => $fileInfo) {
+			if ($filename !== $name) {
+				continue;
+			}
+			if ($fileInfo['content'] === $content) {
+				return $fileInfo['contentId'];
+			}
+		}
+		return false;
+	}
+
+	/**
 	 * Try to determine the mimetype by filename.
 	 * Uses finfo_open() if availble, otherwise guesses it via file extension.
 	 *