Skip to content

Commit

Permalink
refs #127 only add file name suffix for duplicated names
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 23, 2022
1 parent a34a1eb commit 9a8fb3a
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions lib/Service/GoogleDriveAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ public function importFiles(string $userId, string $targetPath,
$nbDownloaded = 0;

foreach ($directoryIdsToExplore as $dirId) {
$conflictingIds = $this->getFilesWithNameConflict($userId, $dirId, $considerSharedFiles);
$params = [
'pageSize' => 1000,
'fields' => implode(',', [
Expand Down Expand Up @@ -338,7 +339,7 @@ public function importFiles(string $userId, string $targetPath,
$saveFolder = $folder;
}

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

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

/**
* @param string $userId
* @param string $dirId
* @param bool $considerSharedFiles
* @return array
*/
private function getFilesWithNameConflict(string $userId, string $dirId, bool $considerSharedFiles): array {
$fileItems = [];
$params = [
'pageSize' => 1000,
'fields' => implode(',', [
'nextPageToken',
'files/id',
'files/name',
'files/parents',
'files/ownedByMe',
]),
'q' => "mimeType!='application/vnd.google-apps.folder' and '" . $dirId . "' in parents",
];
do {
$result = $this->googleApiService->request($userId, 'drive/v3/files', $params);
if (isset($result['error'])) {
return [];
}

$fileItems = array_merge($fileItems, $result['files']);

$params['pageToken'] = $result['nextPageToken'] ?? '';
} while (isset($result['nextPageToken']));

// ignore shared files
if (!$considerSharedFiles) {
$fileItems = array_filter($fileItems, static function (array $fileItem) {
return $fileItem['ownedByMe'];
});
}

// detect duplicates
$nbPerName = array_count_values(
array_map(static function (array $fileItem) {
return $fileItem['name'];
}, $fileItems)
);

return array_map(static function (array $fileItem) {
return $fileItem['id'];
}, array_filter($fileItems, static function (array $fileItem) use ($nbPerName) {
return $nbPerName[$fileItem['name']] > 1;
}));
}

/**
* @param Folder $folder
* @param string $fileName
Expand Down Expand Up @@ -492,9 +544,10 @@ private function downloadAndSaveFile(Folder $saveFolder, string $fileName, strin
/**
* @param array $fileItem
* @param string $userId
* @param bool $hasNameConflict
* @return string name of the file to be saved
*/
private function getFileName(array $fileItem, string $userId): string {
*/
private function getFileName(array $fileItem, string $userId, bool $hasNameConflict): string {
$fileName = preg_replace('/\\n/', '', preg_replace('/\//', '-', $fileItem['name'] ?? 'Untitled'));

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

$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$name = pathinfo($fileName, PATHINFO_FILENAME) . '_' . substr($fileItem['id'], -6);
$name = pathinfo($fileName, PATHINFO_FILENAME);
if ($hasNameConflict) {
$name .= '_' . substr($fileItem['id'], -6);
}

return strlen($extension) ? $name . '.' . $extension : $name;
}
Expand Down

0 comments on commit 9a8fb3a

Please sign in to comment.