| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Uploads; | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  | use BookStack\Model; | 
					
						
							|  |  |  | use BookStack\Traits\HasCreatorAndUpdater; | 
					
						
							| 
									
										
										
										
											2021-06-13 21:16:09 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\BelongsTo; | 
					
						
							| 
									
										
										
										
											2016-10-10 01:58:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 01:29:48 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @property int id | 
					
						
							|  |  |  |  * @property string name | 
					
						
							|  |  |  |  * @property string path | 
					
						
							|  |  |  |  * @property string extension | 
					
						
							| 
									
										
										
										
											2021-06-13 21:16:09 +08:00
										 |  |  |  * @property ?Page page | 
					
						
							| 
									
										
										
										
											2020-09-14 01:29:48 +08:00
										 |  |  |  * @property bool external | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  | class Attachment extends Model | 
					
						
							| 
									
										
										
										
											2016-10-10 01:58:22 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  |     use HasCreatorAndUpdater; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 01:58:22 +08:00
										 |  |  |     protected $fillable = ['name', 'order']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-23 20:36:45 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the downloadable file name for this upload. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-10-23 20:36:45 +08:00
										 |  |  |      * @return mixed|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getFileName() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-14 06:58:40 +08:00
										 |  |  |         if (strpos($this->name, '.') !== false) { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |             return $this->name; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-23 20:36:45 +08:00
										 |  |  |         return $this->name . '.' . $this->extension; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 01:58:22 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the page this file was uploaded to. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-06-13 21:16:09 +08:00
										 |  |  |     public function page(): BelongsTo | 
					
						
							| 
									
										
										
										
											2016-10-10 01:58:22 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo(Page::class, 'uploaded_to'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 03:30:27 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the url of this file. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-06-06 20:55:56 +08:00
										 |  |  |     public function getUrl($openInline = false): string | 
					
						
							| 
									
										
										
										
											2016-10-11 03:30:27 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-20 18:06:10 +08:00
										 |  |  |         if ($this->external && strpos($this->path, 'http') !== 0) { | 
					
						
							|  |  |  |             return $this->path; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-06 20:55:56 +08:00
										 |  |  |         return url('/attachments/' . $this->id . ($openInline ? '?open=true' : '')); | 
					
						
							| 
									
										
										
										
											2016-10-11 03:30:27 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-28 17:45:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 01:29:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Generate a HTML link to this attachment. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-07-28 17:45:28 +08:00
										 |  |  |     public function htmlLink(): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>'; | 
					
						
							| 
									
										
										
										
											2020-07-28 17:45:28 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 01:29:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Generate a markdown link to this attachment. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-07-28 17:45:28 +08:00
										 |  |  |     public function markdownLink(): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         return '[' . $this->name . '](' . $this->getUrl() . ')'; | 
					
						
							| 
									
										
										
										
											2020-07-28 17:45:28 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-10 01:58:22 +08:00
										 |  |  | } |