Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable24] Use '(n)' suffix instead of timestamp prefix for uploaded image names #2377

Merged
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function uploadImage(int $documentId, string $newFileName, $newFileResour
throw new NotPermittedException('No write permissions');
}
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = (string) time() . '-' . $newFileName;
$fileName = $this->getUniqueFileName($saveDir, $newFileName);
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
Expand All @@ -179,7 +179,7 @@ public function uploadImagePublic(?int $documentId, string $newFileName, $newFil
}
$textFile = $this->getTextFilePublic($documentId, $shareToken);
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = (string) time() . '-' . $newFileName;
$fileName = $this->getUniqueFileName($saveDir, $newFileName);
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
Expand Down Expand Up @@ -221,7 +221,7 @@ public function insertImageFile(int $documentId, string $path, string $userId):
private function copyImageFile(File $imageFile, Folder $saveDir, File $textFile): array {
$mimeType = $imageFile->getMimeType();
if (in_array($mimeType, ImageController::IMAGE_MIME_TYPES, true)) {
$fileName = (string) time() . '-' . $imageFile->getName();
$fileName = $this->getUniqueFileName($saveDir, $imageFile->getName());
$targetPath = $saveDir->getPath() . '/' . $fileName;
$targetFile = $imageFile->copy($targetPath);
// get file type and name
Expand All @@ -236,6 +236,30 @@ private function copyImageFile(File $imageFile, Folder $saveDir, File $textFile)
];
}

/**
* Get unique file name in a directory. Add '(n)' suffix.
* @param Folder $dir
* @param string $fileName
* @return string
*/
public static function getUniqueFileName(Folder $dir, string $fileName): string {
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$counter = 1;
$uniqueFileName = $fileName;
if ($extension !== '') {
while ($dir->nodeExists($uniqueFileName)) {
$counter++;
$uniqueFileName = preg_replace('/\.' . $extension . '$/', ' (' . $counter . ').' . $extension, $fileName);
}
} else {
while ($dir->nodeExists($uniqueFileName)) {
$counter++;
$uniqueFileName = preg_replace('/$/', ' (' . $counter . ')', $fileName);
}
}
return $uniqueFileName;
}

/**
* Check if the shared access has write permissions
*
Expand Down
37 changes: 37 additions & 0 deletions tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OCA\Text\AppInfo\Application;
use OCA\Text\Service\ImageService;
use OCP\Files\Folder;

class TextTest extends \PHPUnit\Framework\TestCase {
public function testDummy() {
Expand Down Expand Up @@ -34,4 +35,40 @@ public function testGetAttachmentNamesFromContent() {
$this->assertContains($contentName, $computedNames);
}
}

public function testGetUniqueFileName() {
$fileNameList = [
'foo.png',
'bar',
'plop.png',
'plop (2).png',
'lala.png',
'lala (2).png',
'lala (3).png',
'yay.png',
'yay (2).png',
'yay (3).png',
'yay (5).png',
'file.ext.ext',
];

$folder = $this->createMock(Folder::class);
$folder->expects(self::any())
->method('nodeExists')
->willReturnCallback(function ($name) use ($fileNameList) {
return in_array($name, $fileNameList);
});

// files that do not exist yet
$this->assertEquals('doesNotExistYet', ImageService::getUniqueFileName($folder, 'doesNotExistYet'));
$this->assertEquals('doesNotExistYet.png', ImageService::getUniqueFileName($folder, 'doesNotExistYet.png'));

// files that already exist
$this->assertEquals('foo (2).png', ImageService::getUniqueFileName($folder, 'foo.png'));
$this->assertEquals('bar (2)', ImageService::getUniqueFileName($folder, 'bar'));
$this->assertEquals('plop (3).png', ImageService::getUniqueFileName($folder, 'plop.png'));
$this->assertEquals('lala (4).png', ImageService::getUniqueFileName($folder, 'lala.png'));
$this->assertEquals('yay (4).png', ImageService::getUniqueFileName($folder, 'yay.png'));
$this->assertEquals('file.ext (2).ext', ImageService::getUniqueFileName($folder, 'file.ext.ext'));
}
}