Add a command to clean downloaded images

There were a bug in versions prior to 2.4.0 where images weren't properly removed (mostly when coming from the API).
With that command, we'll be able to remove images which aren't associated to any entries.

Like other command you can pass a username to only clean one user.
This commit is contained in:
Jeremy Benoist
2020-12-14 22:19:19 +01:00
parent 9aeac727df
commit 9743058f7d
2 changed files with 151 additions and 22 deletions

View File

@ -37,6 +37,11 @@ class DownloadImages
$this->setFolder();
}
public function getBaseFolder()
{
return $this->baseFolder;
}
/**
* Process the html and extract images URLs from it.
*
@ -210,6 +215,29 @@ class DownloadImages
@rmdir($folderPath);
}
/**
* Generate the folder where we are going to save images based on the entry url.
*
* @param int $entryId ID of the entry
* @param bool $createFolder Should we create the folder for the given id?
*
* @return string
*/
public function getRelativePath($entryId, $createFolder = true)
{
$hashId = hash('crc32', $entryId);
$relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
$folderPath = $this->baseFolder . '/' . $relativePath;
if (!file_exists($folderPath) && $createFolder) {
mkdir($folderPath, 0777, true);
}
$this->logger->debug('DownloadImages: Folder used for that Entry id', ['folder' => $folderPath, 'entryId' => $entryId]);
return $relativePath;
}
/**
* Get images urls from the srcset image attribute.
*
@ -254,28 +282,6 @@ class DownloadImages
}
}
/**
* Generate the folder where we are going to save images based on the entry url.
*
* @param int $entryId ID of the entry
*
* @return string
*/
private function getRelativePath($entryId)
{
$hashId = hash('crc32', $entryId);
$relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
$folderPath = $this->baseFolder . '/' . $relativePath;
if (!file_exists($folderPath)) {
mkdir($folderPath, 0777, true);
}
$this->logger->debug('DownloadImages: Folder used for that Entry id', ['folder' => $folderPath, 'entryId' => $entryId]);
return $relativePath;
}
/**
* Make an $url absolute based on the $base.
*