ObjectIdDatabaseTableColumn::create('imageID'),
TextDatabaseTableColumn::create('imageUrl')
->notNull(),
- VarcharDatabaseTableColumn::create('imageHash')
+ VarcharDatabaseTableColumn::create('imageUrlHash')
->notNull()
->length(40),
NotNullInt10DatabaseTableColumn::create('width'),
NotNullInt10DatabaseTableColumn::create('height'),
VarcharDatabaseTableColumn::create('imageExtension')
->length(4),
+ DefaultFalseBooleanDatabaseTableColumn::create('isStored')
])
->indices([
DatabaseTablePrimaryIndex::create()
->columns(['imageID']),
- DatabaseTableIndex::create('imageHash')
+ DatabaseTableIndex::create('imageUrlHash')
->type(DatabaseTableIndex::UNIQUE_TYPE)
- ->columns(['imageHash']),
+ ->columns(['imageUrlHash']),
]),
DatabaseTable::create('wcf1_unfurl_url')
* @property-read string $description
* @property-read string $imageHash
* @property-read string $imageUrl
+ * @property-read string $imageUrlHash
* @property-read string $imageExtension
* @property-read int $width
* @property-read int $height
* @property-read int $lastFetch
* @property-read int $imageID
+ * @property-read int $isStored
*/
class UnfurlUrl extends DatabaseObject
{
*/
public function getImageUrl(): ?string
{
- if (!empty($this->imageHash)) {
- $imageFolder = self::IMAGE_DIR . \substr($this->imageHash, 0, 2) . "/";
- $imageName = $this->imageHash . '.' . $this->imageExtension;
+ if ($this->isStored) {
+ $imageFolder = self::IMAGE_DIR . \substr($this->imageUrlHash, 0, 2) . "/";
+ $imageName = $this->imageUrlHash . '.' . $this->imageExtension;
return WCF::getPath() . $imageFolder . $imageName;
} elseif (!empty($this->imageUrl)) {
if ($imageData !== false) {
if ($this->validateImage($imageData)) {
- $imageSaveData['imageUrl'] = StringUtil::trim($unfurlResponse->getImageUrl());
+ $imageSaveData['imageUrl'] = $unfurlResponse->getImageUrl();
+ $imageSaveData['imageUrlHash'] = \sha1($unfurlResponse->getImageUrl());
$imageSaveData['width'] = $imageData[0];
$imageSaveData['height'] = $imageData[1];
if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) {
- $imageSaveData['imageHash'] = $this->saveImage($imageData, $image);
+ $this->saveImage($imageData, $image, $imageSaveData['imageUrlHash']);
$imageSaveData['imageExtension'] = $this->getImageExtension($imageData);
+ $imageSaveData['isStored'] = 1;
}
}
}
{
$sql = "SELECT imageID
FROM wcf" . WCF_N . "_unfurl_url_image
- WHERE imageUrl = ?";
+ WHERE imageUrlHash = ?";
$statement = WCF::getDB()->prepareStatement($sql);
- $statement->execute([$url]);
+ $statement->execute([\sha1($url)]);
$imageID = $statement->fetchSingleColumn();
return true;
}
- private function saveImage(array $imageData, string $image): string
+ private function saveImage(array $imageData, string $image, string $imageHash): string
{
- do {
- $imageHash = StringUtil::getRandomID();
-
- $path = WCF_DIR . UnfurlUrl::IMAGE_DIR . \substr($imageHash, 0, 2) . '/';
- } while (!empty(glob($path . $imageHash . '.*')));
+ $path = WCF_DIR . UnfurlUrl::IMAGE_DIR . \substr($imageHash, 0, 2) . '/';
FileUtil::makePath($path);
CREATE TABLE wcf1_unfurl_url_image (
imageID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
imageUrl TEXT NOT NULL,
- imageHash VARCHAR(40) DEFAULT NULL,
+ imageUrlHash VARCHAR(40) DEFAULT NULL,
width INT(10) NOT NULL,
height INT(10) NOT NULL,
imageExtension VARCHAR(4) DEFAULT NULL,
+ isStored TINYINT(1) NOT NULL DEFAULT 0,
- UNIQUE KEY imageHash (imageHash)
+ UNIQUE KEY imageUrlHash (imageUrlHash)
);
DROP TABLE IF EXISTS wcf1_user;