| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ddomanskyi\LaravelGmail\Services\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ddomanskyi\LaravelGmail\GmailConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ddomanskyi\LaravelGmail\Traits\HasDecodableBody; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Google_Service_Gmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Facades\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Attachment extends GmailConnection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	use HasDecodableBody; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	public $body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	public $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	public $mimeType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public $size; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	public $headerDetails; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	private $headers; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @var Google_Service_Gmail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	private $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	private $messageId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * Attachment constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param $singleMessageId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param  \Google_Service_Gmail_MessagePart  $part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function __construct($singleMessageId, \Google_Service_Gmail_MessagePart $part) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		parent::__construct(config()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->service = new Google_Service_Gmail($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$body = $part->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->id = $body->getAttachmentId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$this->size = $body->getSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->filename = $part->getFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$this->mimeType = $part->getMimeType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->messageId = $singleMessageId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$headers = $part->getHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$this->headerDetails = $this->getHeaderDetails($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * Retuns attachment ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	public function getId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		return $this->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * Returns attachment file name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	public function getFileName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		return $this->filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * Returns mime type of the attachment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 	public function getMimeType() | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		return $this->mimeType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * Returns approximate size of the attachment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	public function getSize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		return $this->size; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @param  string  $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @param  string|null  $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param  string  $disk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	public function saveAttachmentTo($path = null, $filename = null, $disk = 'local') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$data = $this->getDecodedBody($this->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		if (!$data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			throw new \Exception('Could not get the attachment.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$filename = $filename ?: $this->filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		if (is_null($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			$path = '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			if (!Str::endsWith('/', $path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				$path = "{$path}/"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		$filePathAndName = "{$path}{$filename}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		Storage::disk($disk)->put($filePathAndName, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		return $filePathAndName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	public function getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		$attachment = $this->service->users_messages_attachments->get('me', $this->messageId, $this->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		return $attachment->getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 * Returns attachment headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 * Contains Content-ID and X-Attachment-Id for embedded images | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	public function getHeaderDetails($headers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		$headerDetails = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		foreach ($headers as $header) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			$headerDetails[$header->name] = $header->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		return $headerDetails; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |  |