@@ -181,6 +181,39 @@ public function upload(UploadedFile $file, ?string $desiredPath = null): Attachm
181181 ]);
182182 }
183183
184+ /**
185+ * Replace existing file on disk and update database entry.
186+ *
187+ * @throws DestinationAlreadyExistsException
188+ * @throws DisallowedCharacterException
189+ */
190+ public function replace (UploadedFile $ file , Attachment $ attachment ): Attachment
191+ {
192+ $ filename = new Filename ($ file );
193+
194+ $ this ->validateBasename ($ filename );
195+
196+ $ disk = $ this ->getFilesystem ();
197+
198+ $ path = implode ('/ ' , array_filter ([$ attachment ->path , $ filename ]));
199+
200+ if ($ disk ->exists ($ path ) && $ path !== $ attachment ->full_path ) {
201+ throw new DestinationAlreadyExistsException ();
202+ }
203+
204+ $ disk ->delete ($ attachment ->full_path );
205+ $ disk ->put ($ path , $ file ->getContent ());
206+
207+ $ attachment ->update ([
208+ 'name ' => $ filename ->name ,
209+ 'extension ' => $ filename ->extension ,
210+ 'mime_type ' => $ file ->getMimeType (),
211+ 'size ' => $ file ->getSize (),
212+ ]);
213+
214+ return $ attachment ;
215+ }
216+
184217 /**
185218 * Validate filename and throw exception if validation fails.
186219 *
@@ -222,7 +255,7 @@ public function rename(Attachment $file, string $name): void
222255 *
223256 * @throws DestinationAlreadyExistsException if conflicting file exists in desired path.
224257 */
225- public function move (Attachment $ file , string $ desiredPath ): void
258+ public function move (Attachment $ file , ? string $ desiredPath ): void
226259 {
227260 $ disk = $ this ->getFilesystem ();
228261 $ path = "{$ desiredPath }/ {$ file ->filename }" ;
0 commit comments