Skip to content

Commit

Permalink
Don't allow a file to be added to deleting list twice
Browse files Browse the repository at this point in the history
This could happen if you try and delete a file with a croppa-style suffix.  It would be in the array twice, once as the source, and once for matching the pattern.

Fixes #48

Also, I am explicitly checking that files begin with the passed filename.  And I added a new unit test
  • Loading branch information
weotch committed Jul 30, 2014
1 parent db3b3b4 commit e30bc23
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Bkwld/Croppa/Croppa.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ public function findFilesToDelete($url, $delete_original = true) {
$parts = pathinfo($src);
$files = scandir($parts['dirname']);
foreach($files as $file) {
if (strpos($file, $parts['filename']) === false || !preg_match('#'.$this->pattern().'#', $file)) continue;
$deleting[] = $parts['dirname'].'/'.$file;
$path = $parts['dirname'].'/'.$file;
if (strpos($file, $parts['filename']) === 0
&& !in_array($path, $deleting)
&& preg_match('#'.$this->pattern().'#', $file)) {
$deleting[] = $path;
}
}

// Return the list
Expand Down
5 changes: 5 additions & 0 deletions tests/TestDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public function testBadURL() {
$this->assertEquals(0, count($this->croppa->findFilesToDelete('whatever.dude')));
}

// https://github.com/BKWLD/croppa/issues/48
public function testFilenameWithDimensions() {
$this->assertEquals(array(vfsStream::url('dir/me-200x100.jpg')), $this->croppa->findFilesToDelete('me-200x100.jpg'));
}

}

0 comments on commit e30bc23

Please sign in to comment.