- Issue created by @mistergroove
- Status changed to Fixed
5 months ago 5:41am 26 June 2024 - πΊπΈUnited States mfb San Francisco
something like this should work?
$file = \Drupal\file\Entity\File::create(['uri' => 'temporary://my-image.png']); $violations = \Drupal::service('file.validator')->validate($file, ['FileHashDedupe' => []]);
- π¬π§United Kingdom mistergroove
Great. Thanks @mbf I'll give that a go and report back.
- π¬π§United Kingdom mistergroove
OK well that worked great to know it's a duplicated file.. I need to get the file id of the 'already uploaded' file... is there currently a way to just get a list id's of the duplicate existing files?
Or would the only way be to parse the -messageTemplate field?
Symfony\Component\Validator\ConstraintViolationList {#1284 βΌ -violations: array:1 [βΌ 0 => Symfony\Component\Validator \ ConstraintViolation {#1596 βΌ -message: Drupal\Core\StringTranslation \ TranslatableMarkup {#1594 βΆ} -messageTemplate: "This file has already been uploaded as <em class="placeholder"><a href="/admin/content/files/usage/171" target="_blank">17238.jpg</a></em>." -parameters: [] -plural: null -root: Drupal\Core\Entity\Plugin\DataType \ EntityAdapter {#671 βΆ} -propertyPath: "" -invalidValue: Drupal\file\Entity \ File {#1033 βΆ} -constraint: Drupal\filehash\Plugin\Validation\Constraint \ FileHashDedupe {#1494 βΆ} -code: null -cause: null } ] }
- πΊπΈUnited States mfb San Francisco
Howabout calling the File Hash duplicateLookup() method directly?
$fid = \Drupal::service('filehash')->duplicateLookup('sha256', $file);
You'll need to replace 'sha256' with whatever hash algorithm you've enabled.
Automatically closed - issue fixed for 2 weeks with no activity.