Skip to content

Commit ab78970

Browse files
committed
Fix: Implement better filename sanitization utility for Google Drive and Photos services using IFilenameValidator
Signed-off-by: Ahsan Ahmed <ahmedah05@outlook.com>
1 parent d3a81f2 commit ab78970

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

lib/Service/GoogleDriveAPIService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OC\User\NoUserException;
1818
use OCA\Google\AppInfo\Application;
1919
use OCA\Google\BackgroundJob\ImportDriveJob;
20+
use OCA\Google\Service\Utils\FileUtils;
2021
use OCP\BackgroundJob\IJobList;
2122
use OCP\Files\File;
2223
use OCP\Files\Folder;
@@ -540,7 +541,7 @@ private function createDirsUnder(array &$directoriesById, Folder $currentFolder,
540541
// create dir if we are on top OR if its parent is current dir
541542
if (($currentFolderId === '' && !array_key_exists($parentId, $directoriesById))
542543
|| $parentId === $currentFolderId) {
543-
$name = preg_replace('/[\/?<>\\:*|"]/', '-', trim((string)($dir['name'] ?? 'Untitled')));
544+
$name = FileUtils::sanitizeFilename((string)($dir['name']), $id, $this->logger);
544545
if (!$currentFolder->nodeExists($name)) {
545546
$newDir = $currentFolder->newFolder($name);
546547
} else {
@@ -623,7 +624,7 @@ private function downloadAndSaveFile(
623624
* @return string name of the file to be saved
624625
*/
625626
private function getFileName(array $fileItem, string $userId, bool $hasNameConflict): string {
626-
$fileName = preg_replace('/[\/?<>\\:*|"]/', '-', trim((string)($fileItem['name'] ?? 'Untitled')));
627+
$fileName = FileUtils::sanitizeFilename((string)($fileItem['name']), $fileItem['id'], $this->logger);
627628

628629
if (in_array($fileItem['mimeType'], array_values(self::DOCUMENT_MIME_TYPES))) {
629630
$documentFormat = $this->getUserDocumentFormat($userId);

lib/Service/GooglePhotosAPIService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Exception;
1717
use OCA\Google\AppInfo\Application;
1818
use OCA\Google\BackgroundJob\ImportPhotosJob;
19+
use OCA\Google\Service\Utils\FileUtils;
1920
use OCP\BackgroundJob\IJobList;
2021
use OCP\Files\FileInfo;
2122
use OCP\Files\Folder;
@@ -277,7 +278,7 @@ public function importPhotos(
277278
$seenIds = [];
278279
foreach ($albums as $album) {
279280
$albumId = $album['id'];
280-
$albumName = preg_replace('/[\/?<>\\:*|"]/', '-', trim((string)($album['title'] ?? 'Untitled')));
281+
$albumName = FileUtils::sanitizeFilename((string)($album['title']), $album['id'], $this->logger);
281282
if (!$folder->nodeExists($albumName)) {
282283
$albumFolder = $folder->newFolder($albumName);
283284
} else {
@@ -372,7 +373,7 @@ public function importPhotos(
372373
* @throws \OCP\Files\NotPermittedException
373374
*/
374375
private function getPhoto(string $userId, array $photo, Folder $albumFolder): ?int {
375-
$photoName = preg_replace('/[\/?<>\\:*|"]/', '-', trim((string)($photo['filename'] ?? 'Untitled')));
376+
$photoName = FileUtils::sanitizeFilename((string)($photo['filename']), $photo['id'], $this->logger);
376377
if ($albumFolder->nodeExists($photoName)) {
377378
$photoName = $photo['id'] . '_' . $photoName;
378379
}

0 commit comments

Comments
 (0)