Skip to content

Commit 9a8fb3a

Browse files
committed
refs #127 only add file name suffix for duplicated names
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent a34a1eb commit 9a8fb3a

1 file changed

Lines changed: 60 additions & 4 deletions

File tree

lib/Service/GoogleDriveAPIService.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ public function importFiles(string $userId, string $targetPath,
306306
$nbDownloaded = 0;
307307

308308
foreach ($directoryIdsToExplore as $dirId) {
309+
$conflictingIds = $this->getFilesWithNameConflict($userId, $dirId, $considerSharedFiles);
309310
$params = [
310311
'pageSize' => 1000,
311312
'fields' => implode(',', [
@@ -338,7 +339,7 @@ public function importFiles(string $userId, string $targetPath,
338339
$saveFolder = $folder;
339340
}
340341

341-
$fileName = $this->getFileName($fileItem, $userId);
342+
$fileName = $this->getFileName($fileItem, $userId, in_array($fileItem['id'], $conflictingIds));
342343

343344
// If file already exists in folder, don't download unless timestamp is different
344345
if ($saveFolder->nodeExists($fileName)) {
@@ -385,6 +386,57 @@ public function importFiles(string $userId, string $targetPath,
385386
];
386387
}
387388

389+
/**
390+
* @param string $userId
391+
* @param string $dirId
392+
* @param bool $considerSharedFiles
393+
* @return array
394+
*/
395+
private function getFilesWithNameConflict(string $userId, string $dirId, bool $considerSharedFiles): array {
396+
$fileItems = [];
397+
$params = [
398+
'pageSize' => 1000,
399+
'fields' => implode(',', [
400+
'nextPageToken',
401+
'files/id',
402+
'files/name',
403+
'files/parents',
404+
'files/ownedByMe',
405+
]),
406+
'q' => "mimeType!='application/vnd.google-apps.folder' and '" . $dirId . "' in parents",
407+
];
408+
do {
409+
$result = $this->googleApiService->request($userId, 'drive/v3/files', $params);
410+
if (isset($result['error'])) {
411+
return [];
412+
}
413+
414+
$fileItems = array_merge($fileItems, $result['files']);
415+
416+
$params['pageToken'] = $result['nextPageToken'] ?? '';
417+
} while (isset($result['nextPageToken']));
418+
419+
// ignore shared files
420+
if (!$considerSharedFiles) {
421+
$fileItems = array_filter($fileItems, static function (array $fileItem) {
422+
return $fileItem['ownedByMe'];
423+
});
424+
}
425+
426+
// detect duplicates
427+
$nbPerName = array_count_values(
428+
array_map(static function (array $fileItem) {
429+
return $fileItem['name'];
430+
}, $fileItems)
431+
);
432+
433+
return array_map(static function (array $fileItem) {
434+
return $fileItem['id'];
435+
}, array_filter($fileItems, static function (array $fileItem) use ($nbPerName) {
436+
return $nbPerName[$fileItem['name']] > 1;
437+
}));
438+
}
439+
388440
/**
389441
* @param Folder $folder
390442
* @param string $fileName
@@ -492,9 +544,10 @@ private function downloadAndSaveFile(Folder $saveFolder, string $fileName, strin
492544
/**
493545
* @param array $fileItem
494546
* @param string $userId
547+
* @param bool $hasNameConflict
495548
* @return string name of the file to be saved
496-
*/
497-
private function getFileName(array $fileItem, string $userId): string {
549+
*/
550+
private function getFileName(array $fileItem, string $userId, bool $hasNameConflict): string {
498551
$fileName = preg_replace('/\\n/', '', preg_replace('/\//', '-', $fileItem['name'] ?? 'Untitled'));
499552

500553
if (in_array($fileItem['mimeType'], array_values(self::DOCUMENT_MIME_TYPES))) {
@@ -513,7 +566,10 @@ private function getFileName(array $fileItem, string $userId): string {
513566
}
514567

515568
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
516-
$name = pathinfo($fileName, PATHINFO_FILENAME) . '_' . substr($fileItem['id'], -6);
569+
$name = pathinfo($fileName, PATHINFO_FILENAME);
570+
if ($hasNameConflict) {
571+
$name .= '_' . substr($fileItem['id'], -6);
572+
}
517573

518574
return strlen($extension) ? $name . '.' . $extension : $name;
519575
}

0 commit comments

Comments
 (0)