| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LeKoala\FilePond; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Assets\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Assets\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Core\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Control\Director; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\Security\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\View\Requirements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SilverStripe\Versioned\Versioned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SilverStripe\ORM\DataObjectInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SilverStripe\ORM\FieldType\DBHTMLText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SilverStripe\Model\List\SS_List; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SilverStripe\Model\List\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SilverStripe\Core\Validation\ValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * A FilePond field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class FilePondField extends AbstractUploadField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public const IMAGE_MODE_MIN = "min"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public const IMAGE_MODE_MAX = "max"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public const IMAGE_MODE_CROP = "crop"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public const IMAGE_MODE_RESIZE = "resize"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public const IMAGE_MODE_CROP_RESIZE = "crop_resize"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public const DEFAULT_POSTER_HEIGHT = 264; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public const DEFAULT_POSTER_WIDTH = 352; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var array<string> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private static $allowed_actions = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'upload', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         'chunk', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         'revert', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     private static $enable_requirements = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private static $enable_poster = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private static $chunk_by_default = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private static $enable_default_description = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private static $auto_clear_temp_folder = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     private static $auto_clear_threshold = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private static $enable_auto_thumbnails = false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     private static $poster_width = 352; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     private static $poster_height = 264; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @var array<string|int,mixed|null> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected $filePondConfig = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @var array<string,mixed>|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected $customServerConfig = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @var ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     protected $posterHeight = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @var ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     protected $posterWidth = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Create a new file field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param string $name The internal field name, passed to forms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param string $title The field label. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param SS_List $items Items assigned to this field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     public function __construct($name, $title = null, ?SS_List $items = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         parent::__construct($name, $title, $items); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (self::config()->chunk_by_default) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $this->setChunkUploads(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Set a custom config value for this field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @link https://pqina.nl/filepond/docs/patterns/api/filepond-instance/#properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param string $k | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param string|bool|float|int|array<mixed> $v | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function addFilePondConfig($k, $v) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $this->filePondConfig[$k] = $v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @param string $k | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     public function getCustomConfigValue($k, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         if (isset($this->filePondConfig[$k])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             return $this->filePondConfig[$k]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Custom configuration applied to this field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     public function getCustomFilePondConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return $this->filePondConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * Get the value of chunkUploads | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     public function getChunkUploads() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         if (!isset($this->filePondConfig['chunkUploads'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         return $this->filePondConfig['chunkUploads']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * Get the value of customServerConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     public function getCustomServerConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         return $this->customServerConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * Set the value of customServerConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @param array<mixed> $customServerConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     public function setCustomServerConfig(array $customServerConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         $this->customServerConfig = $customServerConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      * Set the value of chunkUploads | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      * Note: please set max file upload first if you want | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * to see the size limit in the description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      * @param bool $chunkUploads | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |     public function setChunkUploads($chunkUploads) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         $this->addFilePondConfig('chunkUploads', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         $this->addFilePondConfig('chunkForce', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         $this->addFilePondConfig('chunkSize', $this->computeMaxChunkSize()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         if ($this->isDefaultMaxFileSize()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |             $this->showDescriptionSize = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |      * @param array<mixed> $sizes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     public function getImageSizeConfigFromArray($sizes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         $mode = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         if (isset($sizes[2])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             $mode = $sizes[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         return $this->getImageSizeConfig($sizes[0], $sizes[1], $mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |      * @param int $width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * @param int $height | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      * @param string $mode min|max|crop|resize|crop_resize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 246 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  |     public function getImageSizeConfig($width, $height, $mode = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  |         if ($mode === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |             $mode = self::IMAGE_MODE_MIN; | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  |         $config = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  |         switch ($mode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 254 |  |  |             case self::IMAGE_MODE_MIN: | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |                 $config['imageValidateSizeMinWidth'] = $width; | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  |                 $config['imageValidateSizeMinHeight'] = $height; | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 258 |  |  |             case self::IMAGE_MODE_MAX: | 
            
                                                                        
                            
            
                                    
            
            
                | 259 |  |  |                 $config['imageValidateSizeMaxWidth'] = $width; | 
            
                                                                        
                            
            
                                    
            
            
                | 260 |  |  |                 $config['imageValidateSizeMaxHeight'] = $height; | 
            
                                                                        
                            
            
                                    
            
            
                | 261 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 262 |  |  |             case self::IMAGE_MODE_CROP: | 
            
                                                                        
                            
            
                                    
            
            
                | 263 |  |  |                 // It crops only to given ratio and tries to keep the largest image | 
            
                                                                        
                            
            
                                    
            
            
                | 264 |  |  |                 $config['allowImageCrop'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 265 |  |  |                 $config['imageCropAspectRatio'] = "{$width}:{$height}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 266 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 267 |  |  |             case self::IMAGE_MODE_RESIZE: | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |                 //  Cover will respect the aspect ratio and will scale to fill the target dimensions | 
            
                                                                        
                            
            
                                    
            
            
                | 269 |  |  |                 $config['allowImageResize'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |                 $config['imageResizeTargetWidth'] = $width; | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |                 $config['imageResizeTargetHeight'] = $height; | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |                 // Don't use these settings and keep api simple | 
            
                                                                        
                            
            
                                    
            
            
                | 274 |  |  |                 // $config['imageResizeMode'] = 'cover'; | 
            
                                                                        
                            
            
                                    
            
            
                | 275 |  |  |                 // $config['imageResizeUpscale'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 276 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 277 |  |  |             case self::IMAGE_MODE_CROP_RESIZE: | 
            
                                                                        
                            
            
                                    
            
            
                | 278 |  |  |                 $config['allowImageResize'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 279 |  |  |                 $config['imageResizeTargetWidth'] = $width; | 
            
                                                                        
                            
            
                                    
            
            
                | 280 |  |  |                 $config['imageResizeTargetHeight'] = $height; | 
            
                                                                        
                            
            
                                    
            
            
                | 281 |  |  |                 $config['allowImageCrop'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 282 |  |  |                 $config['imageCropAspectRatio'] = "{$width}:{$height}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 283 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 284 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 285 |  |  |                 throw new Exception("Unsupported '$mode' mode"); | 
            
                                                                        
                            
            
                                    
            
            
                | 286 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 287 |  |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |      * @link https://pqina.nl/filepond/docs/api/plugins/image-crop/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |      * @link https://pqina.nl/filepond/docs/api/plugins/image-resize/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |      * @link https://pqina.nl/filepond/docs/api/plugins/image-validate-size/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |      * @param int $width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |      * @param int $height | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * @param string $mode min|max|crop|resize|crop_resize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |     public function setImageSize($width, $height, $mode = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |         $config = $this->getImageSizeConfig($width, $height, $mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         foreach ($config as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |             $this->addFilePondConfig($k, $v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         // We need a custom poster size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |         $this->adjustPosterSize($width, $height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      * This is a frontend alternative to setRenamePattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      * @link https://pqina.nl/filepond/docs/api/plugins/file-rename/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |      * @param string $name The name (extension is added automatically) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |     public function setRenameFile($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |         $this->addFilePondConfig('fileRenameFunction', $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |      * @param int $width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |      * @param int $height | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |     protected function adjustPosterSize($width, $height) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |         // If the height is smaller than our default, make smaller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |         if ($height < self::getDefaultPosterHeight()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |             $this->posterHeight = $height; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |             $this->posterWidth = $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |             // Adjust width to keep aspect ratio with our default height | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |             $ratio = $height / self::getDefaultPosterHeight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |             //@phpstan-ignore-next-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |             $this->posterWidth = round($width / $ratio); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |     public function getPosterWidth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |         if ($this->posterWidth) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |             return $this->posterWidth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |         return self::getDefaultPosterWidth(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |     public function getPosterHeight() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |         if ($this->posterHeight) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |             return $this->posterHeight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |         return self::getDefaultPosterHeight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |      * Return the config applied for this field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |      * Typically converted to json and set in a data attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |      * @return array<string,mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |     public function getFilePondConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |         $this->fixName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |         $name = $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         $multiple = $this->getIsMultiUpload(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         $i18nConfig = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |             'labelIdle' => _t('FilePondField.labelIdle', 'Drag & Drop your files or <span class="filepond--label-action"> Browse </span>'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |             'labelFileProcessing' => _t('FilePondField.labelFileProcessing', 'Uploading'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |             'labelFileProcessingComplete' => _t('FilePondField.labelFileProcessingComplete', 'Upload complete'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |             'labelFileProcessingAborted' => _t('FilePondField.labelFileProcessingAborted', 'Upload cancelled'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |             'labelTapToCancel' => _t('FilePondField.labelTapToCancel', 'tap to cancel'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |             'labelTapToRetry' => _t('FilePondField.labelTapToCancel', 'tap to retry'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |             'labelTapToUndo' => _t('FilePondField.labelTapToCancel', 'tap to undo'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |         // Base config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |         $config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |             'name' => $name, // This will also apply to the hidden fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |             'allowMultiple' => $multiple, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |             'maxFiles' => $this->getAllowedMaxFileNumber(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |             'server' => $this->getServerOptions(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |             'files' => $this->getExistingUploadsData(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |         $maxFileSize = $this->getMaxFileSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |         if ($maxFileSize) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |             $config['maxFileSize'] = $maxFileSize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |         $acceptedFileTypes = $this->getAcceptedFileTypes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |         if (!empty($acceptedFileTypes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |             $config['acceptedFileTypes'] = array_values($acceptedFileTypes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |         // image poster | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |         // @link https://pqina.nl/filepond/docs/api/plugins/file-poster/#usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |         if (self::config()->enable_poster) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |             $config['filePosterHeight'] = self::config()->poster_height ?? self::DEFAULT_POSTER_HEIGHT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |         // image validation/crop based on record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |         /** @var DataObject|null $record */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |         $record = $this->getForm()->getRecord(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |         if ($record) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |             $sizes = $record->config()->image_sizes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |             $name = $this->getSafeName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |             if ($sizes && isset($sizes[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |                 $newConfig = $this->getImageSizeConfigFromArray($sizes[$name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |                 $config = array_merge($config, $newConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |                 $this->adjustPosterSize($sizes[$name][0], $sizes[$name][1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |         // Any custom setting will override the base ones | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |         $config = array_merge($config, $i18nConfig, $this->filePondConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |      * Compute best size for chunks based on server settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  |      * @return float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |     protected function computeMaxChunkSize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |         $upload_max_filesize = ini_get('upload_max_filesize'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |         $post_max_size = ini_get('post_max_size'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |         $upload_max_filesize = $upload_max_filesize ? $upload_max_filesize : "2MB"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |         $post_max_size = $post_max_size ? $post_max_size : "2MB"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |         $maxUpload = Convert::memstring2bytes($upload_max_filesize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |         $maxPost = Convert::memstring2bytes($post_max_size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |         // ~90%, allow some overhead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  |         return round(min($maxUpload, $maxPost) * 0.9); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |      * @param array<mixed>|int|string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |      * @param DataObject|array<string,mixed> $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |     public function setValue($value, $record = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |         // Normalize values to something similar to UploadField usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |         if (is_numeric($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |             $value = ['Files' => [$value]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |         } elseif (is_array($value) && empty($value['Files'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |             // make sure we don't assign {"name":"","full_path":"","type":"","tmp_name":"","error":4,"size":0} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |             // if $_FILES is not empty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |             if (isset($value['tmp_name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |                 $value = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |             $value = ['Files' => $value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  |         if ($record) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |             $name = $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |             if ($record instanceof DataObject && $record->hasMethod($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |                 $data = $record->$name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |                 // Wrap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |                 if ($data instanceof DataObject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |                     $data = new ArrayList([$data]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |                 foreach ($data as $uploadedItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |                     $this->trackFileID($uploadedItem->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |         //@phpstan-ignore-next-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |         return parent::setValue($value, $record); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |      * Get the currently used form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |      * @return Form|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |     public function getForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |         return $this->form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  |      * Configure our endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |      * @link https://pqina.nl/filepond/docs/patterns/api/server/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |     public function getServerOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |         if (!empty($this->customServerConfig)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |             return $this->customServerConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |         if (!$this->getForm()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |             throw new LogicException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |                 'Field must be associated with a form to call getServerOptions(). Please use $field->setForm($form);' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |         $endpoint = $this->getChunkUploads() ? 'chunk' : 'upload'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |         $server = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |             'process' => $this->getUploadEnabled() ? $this->getLinkParameters($endpoint) : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |             'fetch' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |             'revert' => $this->getUploadEnabled() ? $this->getLinkParameters('revert') : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |         if ($this->getUploadEnabled() && $this->getChunkUploads()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |             $server['fetch'] =  $this->getLinkParameters($endpoint . "?fetch="); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |             $server['patch'] =  $this->getLinkParameters($endpoint . "?patch="); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |         return $server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |      * Configure the following parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  |      * url : Path to the end point | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |      * method : Request method to use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  |      * withCredentials : Toggles the XMLHttpRequest withCredentials on or off | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |      * headers : An object containing additional headers to send | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |      * timeout : Timeout for this action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |      * onload : Called when server response is received, useful for getting the unique file id from the server response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |      * onerror : Called when server error is received, receis the response body, useful to select the relevant error data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |     protected function getLinkParameters($action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |         $form = $this->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |         $token = $form->getSecurityToken()->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |         /** @var DataObject|null $record */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  |         $record = $form->getRecord(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |         $headers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |             'X-SecurityID' => $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |         // Allow us to track the record instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |         if ($record) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  |             $headers['X-RecordClassName'] = get_class($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  |             $headers['X-RecordID'] = $record->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |             'url' => $this->Link($action), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  |             'headers' => $headers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 |  |  |      * The maximum size of a file, for instance 5MB or 750KB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 |  |  |      * Suitable for JS usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  |     public function getMaxFileSize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  |         $size = $this->getValidator()->getAllowedMaxFileSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  |         if (!$size) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |         // Only supports KB and MB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |         if ($size < 1024 * 1024) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |             $size = round($size / 1024) . ' KB'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |             $size = round($size / (1024 * 1024)) . ' MB'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 |  |  |         return str_replace(' ', '', $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |      * Set initial values to FilePondField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |      * See: https://pqina.nl/filepond/docs/patterns/api/filepond-object/#setting-initial-files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 |  |  |     public function getExistingUploadsData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 |  |  |         // Both Value() & dataValue() seem to return an array eg: ['Files' => [258, 259, 257]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |         $fileIDarray = $this->getValue() ?: ['Files' => []]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 |  |  |         if (!isset($fileIDarray['Files']) || !count($fileIDarray['Files'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 597 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 598 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 599 |  |  |         $existingUploads = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 600 |  |  |         foreach ($fileIDarray['Files'] as $fileID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 601 |  |  |             $file = $this->getFileByID($fileID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 602 |  |  |             if (!$file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 603 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 604 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 605 |  |  |             $existingUpload = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 606 |  |  |                 // the server file reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 607 |  |  |                 'source' => (int) $fileID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 608 |  |  |                 // set type to local to indicate an already uploaded file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 609 |  |  |                 'options' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 610 |  |  |                     'type' => 'local', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 611 |  |  |                     // file information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 612 |  |  |                     'file' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 613 |  |  |                         'name' => $file->Name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 614 |  |  |                         'size' => (int) $file->getAbsoluteSize(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 615 |  |  |                         'type' => $file->getMimeType(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 616 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 617 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 618 |  |  |                 'metadata' => [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 619 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 620 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 621 |  |  |             // Show poster | 
            
                                                                                                            
                            
            
                                    
            
            
                | 622 |  |  |             // @link https://pqina.nl/filepond/docs/api/plugins/file-poster/#usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 623 |  |  |             if (self::config()->enable_poster && $file instanceof Image && $file->ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 624 |  |  |                 // Size matches the one from asset admin or from or set size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 625 |  |  |                 $w = self::getDefaultPosterWidth(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 626 |  |  |                 if ($this->posterWidth) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 627 |  |  |                     $w = $this->posterWidth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 628 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 629 |  |  |                 $h = self::getDefaultPosterHeight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 630 |  |  |                 if ($this->posterHeight) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 631 |  |  |                     $h = $this->posterHeight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 632 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 633 |  |  |                 /** @var Image|null $resizedImage */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 634 |  |  |                 $resizedImage = $file->Fill($w, $h); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 635 |  |  |                 if ($resizedImage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 636 |  |  |                     $poster = $resizedImage->getAbsoluteURL(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 637 |  |  |                     $existingUpload['options']['metadata']['poster'] = $poster; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 638 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 639 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 640 |  |  |             $existingUploads[] = $existingUpload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 641 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 642 |  |  |         return $existingUploads; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 643 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 644 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 645 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 646 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 647 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 648 |  |  |     public static function getDefaultPosterWidth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 649 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 650 |  |  |         return self::config()->poster_width ?? self::DEFAULT_POSTER_WIDTH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 651 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 652 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 653 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 654 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 655 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 656 |  |  |     public static function getDefaultPosterHeight() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 657 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 658 |  |  |         return self::config()->poster_height ?? self::DEFAULT_POSTER_HEIGHT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 659 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 660 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 661 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 662 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 663 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 664 |  |  |     public static function Requirements() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 665 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 666 |  |  |         // It includes css styles already | 
            
                                                                                                            
                            
            
                                    
            
            
                | 667 |  |  |         Requirements::javascript('lekoala/silverstripe-filepond: client/filepond-input.min.js'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 668 |  |  |         Requirements::css('lekoala/silverstripe-filepond: client/filepond.css'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 669 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 670 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 671 |  |  |     public function getAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 672 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 673 |  |  |         // don't use parent as it will include data-schema that we don'tt need | 
            
                                                                                                            
                            
            
                                    
            
            
                | 674 |  |  |         $attributes = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 675 |  |  |             'class' => $this->extraClass(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 676 |  |  |             'type' => 'file', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 677 |  |  |             'multiple' => $this->getIsMultiUpload(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 678 |  |  |             'id' => $this->ID(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 679 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 680 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 681 |  |  |         $attributes = array_merge($attributes, $this->attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 682 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 683 |  |  |         $this->fixName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 684 |  |  |         $attributes['name'] = $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 685 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 686 |  |  |         $this->extend('updateAttributes', $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 687 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 688 |  |  |         return $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 689 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 690 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 691 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 692 |  |  |      * Make sure the name is correct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 693 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 694 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 695 |  |  |     protected function fixName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 696 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 697 |  |  |         $name = $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 698 |  |  |         $multiple = $this->getIsMultiUpload(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 699 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 700 |  |  |         // Multi uploads need [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 701 |  |  |         if ($multiple && strpos($name, '[]') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 702 |  |  |             $name .= '[]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 703 |  |  |             $this->setName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 704 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 705 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 706 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 707 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 708 |  |  |      * @param array<string,mixed> $properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 709 |  |  |      * @return DBHTMLText | 
            
                                                                                                            
                            
            
                                    
            
            
                | 710 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 711 |  |  |     public function FieldHolder($properties = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 712 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 713 |  |  |         if (self::config()->enable_requirements) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 714 |  |  |             self::Requirements(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 715 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 716 |  |  |         return parent::FieldHolder($properties); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 717 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 718 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 719 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 720 |  |  |      * @param array<mixed> $properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 721 |  |  |      * @return DBHTMLText|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 722 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 723 |  |  |     public function Field($properties = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 724 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 725 |  |  |         $html = parent::Field($properties); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 726 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 727 |  |  |         $config = $this->getFilePondConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 728 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 729 |  |  |         // Simply wrap with custom element and set config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 730 |  |  |         $html = "<filepond-input data-config='" . json_encode($config) . "'>" . $html . '</filepond-input>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 731 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 732 |  |  |         return $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 733 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 734 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 735 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 736 |  |  |      * Check the incoming request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 737 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 738 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 739 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 740 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 741 |  |  |     public function prepareUpload(HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 742 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 743 |  |  |         $name = $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 744 |  |  |         $tmpFile = $request->postVar($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 745 |  |  |         if (!$tmpFile) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 746 |  |  |             throw new RuntimeException("No file"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 747 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 748 |  |  |         $tmpFile = $this->normalizeTempFile($tmpFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 749 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 750 |  |  |         // Update $tmpFile with a better name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 751 |  |  |         if ($this->renamePattern) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 752 |  |  |             $tmpFile['name'] = $this->changeFilenameWithPattern( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 753 |  |  |                 $tmpFile['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 754 |  |  |                 $this->renamePattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 755 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 756 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 757 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 758 |  |  |         return $tmpFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 759 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 760 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 761 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 762 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 763 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 764 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 765 |  |  |     protected function securityChecks(HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 766 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 767 |  |  |         if ($this->isDisabled() || $this->isReadonly()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 768 |  |  |             throw new RuntimeException("Field is disabled or readonly"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 769 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 770 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 771 |  |  |         // CSRF check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 772 |  |  |         $token = $this->getForm()->getSecurityToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 773 |  |  |         if (!$token->checkRequest($request)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 774 |  |  |             throw new RuntimeException("Invalid token"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 775 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 776 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 777 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 778 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 779 |  |  |      * @param File $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 780 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 781 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 782 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 783 |  |  |     protected function setFileDetails(File $file, HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 784 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 785 |  |  |         // Mark as temporary until properly associated with a record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 786 |  |  |         // Files will be unmarked later on by saveInto method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 787 |  |  |         $file->IsTemporary = true; //@phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 788 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 789 |  |  |         // We can also track the record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 790 |  |  |         $RecordID = $request->getHeader('X-RecordID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 791 |  |  |         $RecordClassName = $request->getHeader('X-RecordClassName'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 792 |  |  |         if (!$file->ObjectID) { //@phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 793 |  |  |             $file->ObjectID = $RecordID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 794 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 795 |  |  |         if (!$file->ObjectClass) { //@phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 796 |  |  |             $file->ObjectClass = $RecordClassName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 797 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 798 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 799 |  |  |         if ($file->isChanged()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 800 |  |  |             // If possible, prevent creating a version for no reason | 
            
                                                                                                            
                            
            
                                    
            
            
                | 801 |  |  |             // @link https://docs.silverstripe.org/en/4/developer_guides/model/versioning/#writing-changes-to-a-versioned-dataobject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 802 |  |  |             if ($file->hasExtension(Versioned::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 803 |  |  |                 $file->writeWithoutVersion(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 804 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 805 |  |  |                 $file->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 806 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 807 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 808 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 809 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 810 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 811 |  |  |      * Creates a single file based on a form-urlencoded upload. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 812 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 813 |  |  |      * 1 client uploads file my-file.jpg as multipart/form-data using a POST request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 814 |  |  |      * 2 server saves file to unique location tmp/12345/my-file.jpg | 
            
                                                                                                            
                            
            
                                    
            
            
                | 815 |  |  |      * 3 server returns unique location id 12345 in text/plain response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 816 |  |  |      * 4 client stores unique id 12345 in a hidden input field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 817 |  |  |      * 5 client submits the FilePond parent form containing the hidden input field with the unique id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 818 |  |  |      * 6 server uses the unique id to move tmp/12345/my-file.jpg to its final location and remove the tmp/12345 folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 819 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 820 |  |  |      * Along with the file object, FilePond also sends the file metadata to the server, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 821 |  |  |      * both these objects are given the same name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 822 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 823 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 824 |  |  |      * @return HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 825 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 826 |  |  |     public function upload(HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 827 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 828 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 829 |  |  |             $this->securityChecks($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 830 |  |  |             $tmpFile = $this->prepareUpload($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 831 |  |  |         } catch (Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 832 |  |  |             return $this->httpError(400, $ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 833 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 834 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 835 |  |  |         $file = $this->saveTemporaryFile($tmpFile, $error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 836 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 837 |  |  |         // Handle upload errors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 838 |  |  |         if ($error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 839 |  |  |             $this->getUpload()->clearErrors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 840 |  |  |             $jsonError = json_encode($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 841 |  |  |             $jsonError = $jsonError ? $jsonError : json_last_error_msg(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 842 |  |  |             return $this->httpError(400, $jsonError); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 843 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 844 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 845 |  |  |         // File can be an AssetContainer and not a DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 846 |  |  |         if ($file instanceof DataObject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 847 |  |  |             $this->setFileDetails($file, $request); //@phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 848 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 849 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 850 |  |  |         $this->getUpload()->clearErrors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 851 |  |  |         $fileId = $file->ID; //@phpstan-ignore-line | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 852 |  |  |         $this->trackFileID($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 853 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 854 |  |  |         if (self::config()->auto_clear_temp_folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 855 |  |  |             // Set a limit of 100 because otherwise it would be really slow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 856 |  |  |             self::clearTemporaryUploads(true, 100); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 857 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 858 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 859 |  |  |         // server returns unique location id 12345 in text/plain response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 860 |  |  |         $response = new HTTPResponse($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 861 |  |  |         $response->addHeader('Content-Type', 'text/plain'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 862 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 863 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 864 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 865 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 866 |  |  |      * @link https://pqina.nl/filepond/docs/api/server/#process-chunks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 867 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 868 |  |  |      * @return HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 869 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 870 |  |  |     public function chunk(HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 871 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 872 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 873 |  |  |             $this->securityChecks($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 874 |  |  |         } catch (Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 875 |  |  |             return $this->httpError(400, $ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 876 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 877 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 878 |  |  |         $method = $request->httpMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 879 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 880 |  |  |         // The random token is returned as a query string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 881 |  |  |         $id = $request->getVar('patch'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 882 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 883 |  |  |         // FilePond will send a POST request (without file) to start a chunked transfer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 884 |  |  |         // expecting to receive a unique transfer id in the response body, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 885 |  |  |         // it'll add the Upload-Length header to this request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 886 |  |  |         if ($method == "POST") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 887 |  |  |             // Initial post payload doesn't contain name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 888 |  |  |             // It would be better to return some kind of random token instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 889 |  |  |             // But FilePond stores the id upon the first request :-( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 890 |  |  |             $file = new File(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 891 |  |  |             $this->setFileDetails($file, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 892 |  |  |             $fileId = $file->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 893 |  |  |             $this->trackFileID($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 894 |  |  |             $response = new HTTPResponse((string)$fileId, 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 895 |  |  |             $response->addHeader('Content-Type', 'text/plain'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 896 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 897 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 898 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 899 |  |  |         // location of patch files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 900 |  |  |         $filePath = TEMP_PATH . "/filepond-" . $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 901 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 902 |  |  |         // FilePond will send a HEAD request to determine which chunks have already been uploaded, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 903 |  |  |         // expecting the file offset of the next expected chunk in the Upload-Offset response header. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 904 |  |  |         if ($method == "HEAD") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 905 |  |  |             $nextOffset = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 906 |  |  |             while (is_file($filePath . '.patch.' . $nextOffset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 907 |  |  |                 $nextOffset++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 908 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 909 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 910 |  |  |             $response = new HTTPResponse((string)$nextOffset, 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 911 |  |  |             $response->addHeader('Content-Type', 'text/plain'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 912 |  |  |             $response->addHeader('Upload-Offset', (string)$nextOffset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 913 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 914 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 915 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 916 |  |  |         // FilePond will send a PATCH request to push a chunk to the server. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 917 |  |  |         // Each of these requests is accompanied by a Content-Type, Upload-Offset, Upload-Name, and Upload-Length header. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 918 |  |  |         if ($method != "PATCH") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 919 |  |  |             return $this->httpError(400, "Invalid method"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 920 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 921 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 922 |  |  |         // The name of the file being transferred | 
            
                                                                                                            
                            
            
                                    
            
            
                | 923 |  |  |         $uploadName = $request->getHeader('Upload-Name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 924 |  |  |         // The offset of the chunk being transferred (starts with 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 925 |  |  |         $offset = $request->getHeader('Upload-Offset'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 926 |  |  |         // The total size of the file being transferred (in bytes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 927 |  |  |         $length = (int) $request->getHeader('Upload-Length'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 928 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 929 |  |  |         // should be numeric values, else exit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 930 |  |  |         if (!is_numeric($offset) || !is_numeric($length)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 931 |  |  |             return $this->httpError(400, "Invalid offset or length"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 932 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 933 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 934 |  |  |         // write patch file for this request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 935 |  |  |         file_put_contents($filePath . '.patch.' . $offset, $request->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 936 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 937 |  |  |         // calculate total size of patches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 938 |  |  |         $size = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 939 |  |  |         $patch = glob($filePath . '.patch.*'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 940 |  |  |         if ($patch) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 941 |  |  |             foreach ($patch as $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 942 |  |  |                 $size += filesize($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 943 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 944 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 945 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 946 |  |  |         // check if we are above our size limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 947 |  |  |         $maxAllowedSize = $this->getValidator()->getAllowedMaxFileSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 948 |  |  |         if ($maxAllowedSize && $size > $maxAllowedSize) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 949 |  |  |             return $this->httpError(400, "File must not be larger than " . $this->getMaxFileSize()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 950 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 951 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 952 |  |  |         // if total size equals length of file we have gathered all patch files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 953 |  |  |         if ($size >= $length) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 954 |  |  |             // create output file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 955 |  |  |             $outputFile = fopen($filePath, 'wb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 956 |  |  |             if ($patch && $outputFile) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 957 |  |  |                 // write patches to file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 958 |  |  |                 foreach ($patch as $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 959 |  |  |                     // get offset from filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 960 |  |  |                     list($dir, $offset) = explode('.patch.', $filename, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 961 |  |  |                     // read patch and close | 
            
                                                                                                            
                            
            
                                    
            
            
                | 962 |  |  |                     $patchFile = fopen($filename, 'rb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 963 |  |  |                     $patchFileSize = filesize($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 964 |  |  |                     if ($patchFile && $patchFileSize) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 965 |  |  |                         $patchContent = fread($patchFile, $patchFileSize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 966 |  |  |                         if ($patchContent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 967 |  |  |                             fclose($patchFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 968 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 969 |  |  |                             // apply patch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 970 |  |  |                             fseek($outputFile, (int) $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 971 |  |  |                             fwrite($outputFile, $patchContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 972 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 973 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 974 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 975 |  |  |                 // remove patches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 976 |  |  |                 foreach ($patch as $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 977 |  |  |                     unlink($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 978 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 979 |  |  |                 // done with file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 980 |  |  |                 fclose($outputFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 981 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 982 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 983 |  |  |             // Finalize real filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 984 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 985 |  |  |             // We need to class this as it mutates the state and set the record if any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 986 |  |  |             $relationClass = $this->getRelationAutosetClass(File::class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 987 |  |  |             $realFilename = $this->getFolderName() . "/" . $uploadName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 988 |  |  |             if ($this->renamePattern) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 989 |  |  |                 $realFilename = $this->changeFilenameWithPattern( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 990 |  |  |                     $realFilename, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 991 |  |  |                     $this->renamePattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 992 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 993 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 994 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 995 |  |  |             // write output file to asset store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 996 |  |  |             $file = $this->getFileByID($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 997 |  |  |             if (!$file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 998 |  |  |                 return $this->httpError(400, "File $id not found"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 999 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1000 |  |  |             $file->setFromLocalFile($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1001 |  |  |             $file->setFilename($realFilename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1002 |  |  |             $file->Title = $uploadName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1003 |  |  |             // Set proper class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1004 |  |  |             $relationClass = File::get_class_for_file_extension( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1005 |  |  |                 File::get_file_extension($realFilename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1006 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1007 |  |  |             $file->setClassName($relationClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1008 |  |  |             $file->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1009 |  |  |             // Reload file instance to get the right class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1010 |  |  |             // it is not cached so we should get a fresh record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1011 |  |  |             $file = $this->getFileByID($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1012 |  |  |             // since we don't go through our upload object, call extension manually | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1013 |  |  |             $file->extend('onAfterUpload'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1014 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1015 |  |  |             // Cleanup temp files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1016 |  |  |             $patch = glob($filePath . '.patch.*'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1017 |  |  |             if ($patch) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1018 |  |  |                 foreach ($patch as $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1019 |  |  |                     unlink($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1020 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1021 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1022 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1023 |  |  |         $response = new HTTPResponse('', 204); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1024 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1025 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1026 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1027 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1028 |  |  |      * @link https://pqina.nl/filepond/docs/api/server/#revert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1029 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1030 |  |  |      * @return HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1031 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1032 |  |  |     public function revert(HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1033 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1034 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1035 |  |  |             $this->securityChecks($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1036 |  |  |         } catch (Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1037 |  |  |             return $this->httpError(400, $ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1038 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1039 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1040 |  |  |         $method = $request->httpMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1041 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1042 |  |  |         if ($method != "DELETE") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1043 |  |  |             return $this->httpError(400, "Invalid method"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1044 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1045 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1046 |  |  |         $fileID = (int) $request->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1047 |  |  |         if (!in_array($fileID, $this->getTrackedIDs())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1048 |  |  |             return $this->httpError(400, "Invalid ID"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1049 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1050 |  |  |         $file = $this->getFileByID($fileID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1051 |  |  |         if (!$file->IsTemporary) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1052 |  |  |             return $this->httpError(400, "Invalid file"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1053 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1054 |  |  |         if (!$file->canDelete()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1055 |  |  |             return $this->httpError(400, "Cannot delete file"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1056 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1057 |  |  |         $file->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1058 |  |  |         $response = new HTTPResponse('', 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1059 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1060 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1061 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1062 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1063 |  |  |      * Clear temp folder that should not contain any file other than temporary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1064 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1065 |  |  |      * @param boolean $doDelete Set to true to actually delete the files, otherwise it's just a dry-run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1066 |  |  |      * @param int $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1067 |  |  |      * @return File[] List of files removed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1068 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1069 |  |  |     public static function clearTemporaryUploads($doDelete = false, $limit = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1070 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1071 |  |  |         $tempFiles = File::get()->filter('IsTemporary', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1072 |  |  |         if ($limit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1073 |  |  |             $tempFiles = $tempFiles->limit($limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1074 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1075 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1076 |  |  |         $threshold = self::config()->auto_clear_threshold; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1077 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1078 |  |  |         // Set a default threshold if none set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1079 |  |  |         if (!$threshold) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1080 |  |  |             if (Director::isDev()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1081 |  |  |                 $threshold = '-10 minutes'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1082 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1083 |  |  |                 $threshold = '-1 day'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1084 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1085 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1086 |  |  |         if (is_int($threshold)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1087 |  |  |             $thresholdTime = time() - $threshold; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1088 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1089 |  |  |             $thresholdTime = strtotime($threshold); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1090 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1091 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1092 |  |  |         // Update query to avoid fetching unecessary records | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1093 |  |  |         $tempFiles = $tempFiles->filter("Created:LessThan", date('Y-m-d H:i:s', $thresholdTime)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1094 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1095 |  |  |         $filesDeleted = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1096 |  |  |         foreach ($tempFiles as $tempFile) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1097 |  |  |             $createdTime = strtotime($tempFile->Created); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1098 |  |  |             if ($createdTime < $thresholdTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1099 |  |  |                 $filesDeleted[] = $tempFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1100 |  |  |                 if ($doDelete) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1101 |  |  |                     if ($tempFile->hasExtension(Versioned::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1102 |  |  |                         $tempFile->deleteFromStage(Versioned::LIVE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1103 |  |  |                         $tempFile->deleteFromStage(Versioned::DRAFT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1104 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1105 |  |  |                         $tempFile->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1106 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1107 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1110 |  |  |         return $filesDeleted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1114 |  |  |      * Allows tracking uploaded ids to prevent unauthorized attachements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1116 |  |  |      * @param int|string $fileId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1117 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1119 |  |  |     public function trackFileID($fileId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1121 |  |  |         $fileId = is_string($fileId) ? intval($fileId) : $fileId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1122 |  |  |         $session = $this->getRequest()->getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1123 |  |  |         $uploadedIDs = $this->getTrackedIDs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1124 |  |  |         if (!in_array($fileId, $uploadedIDs)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1125 |  |  |             $uploadedIDs[] = $fileId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1127 |  |  |         $session->set('FilePond', $uploadedIDs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1131 |  |  |      * Get all authorized tracked ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1132 |  |  |      * @return array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1134 |  |  |     public function getTrackedIDs() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1136 |  |  |         $session = $this->getRequest()->getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1137 |  |  |         $uploadedIDs = $session->get('FilePond'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1138 |  |  |         if ($uploadedIDs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1139 |  |  |             return $uploadedIDs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1141 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1144 |  |  |     public function saveInto(DataObjectInterface $record) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1146 |  |  |         // Note that the list of IDs is based on the value sent by the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1147 |  |  |         // It can be spoofed because checks are minimal (by default, canView = true and only check if isInDB) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1148 |  |  |         $IDs = $this->getItemIDs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1150 |  |  |         $Member = Security::getCurrentUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1152 |  |  |         // Ensure the files saved into the DataObject have been tracked (either because already on the DataObject or uploaded by the user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1153 |  |  |         $trackedIDs = $this->getTrackedIDs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1154 |  |  |         foreach ($IDs as $ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1155 |  |  |             if (!in_array($ID, $trackedIDs)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1156 |  |  |                 throw new ValidationException("Invalid file ID : $ID"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1157 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1160 |  |  |         // Move files out of temporary folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1161 |  |  |         foreach ($IDs as $ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1162 |  |  |             $file = $this->getFileByID($ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1163 |  |  |             //@phpstan-ignore-next-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1164 |  |  |             if ($file && $file->IsTemporary) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1165 |  |  |                 // The record does not have an ID which is a bad idea to attach the file to it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1166 |  |  |                 if (!$record->ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1167 |  |  |                     $record->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1168 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1169 |  |  |                 // Check if the member is owner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1170 |  |  |                 if ($Member && $Member->ID != $file->OwnerID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1171 |  |  |                     throw new ValidationException("Failed to authenticate owner"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1172 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1173 |  |  |                 $file->IsTemporary = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1174 |  |  |                 $file->ObjectID = $record->ID; //@phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1175 |  |  |                 $file->ObjectClass = get_class($record); //@phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1176 |  |  |                 $file->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1177 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1178 |  |  |                 // File was uploaded earlier, no need to do anything | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1179 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1182 |  |  |         // Proceed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1183 |  |  |         parent::saveInto($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1185 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1188 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1189 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1191 |  |  |     public function Type() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1193 |  |  |         return 'filepond'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1194 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 1195 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 1196 |  |  |  |