| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dthumbnailprovider.h" |
| 6 |
#include <DObjectPrivate> |
| 7 |
|
| 8 |
#include <QCryptographicHash> |
| 9 |
#include <QDir> |
| 10 |
#include <QDateTime> |
| 11 |
#include <QImageReader> |
| 12 |
#include <QQueue> |
| 13 |
#include <QMimeType> |
| 14 |
#include <QMimeDatabase> |
| 15 |
#include <QReadWriteLock> |
| 16 |
#include <QWaitCondition> |
| 17 |
#include <QPainter> |
| 18 |
#include <QUrl> |
| 19 |
#include <QDebug> |
| 20 |
|
| 21 |
#include <DStandardPaths> |
| 22 |
|
| 23 |
DGUI_BEGIN_NAMESPACE |
| 24 |
|
| 25 |
#define FORMAT ".png" |
| 26 |
#define THUMBNAIL_PATH \ |
| 27 |
DCORE_NAMESPACE::DStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/thumbnails" |
| 28 |
#define THUMBNAIL_FAIL_PATH THUMBNAIL_PATH"/fail" |
| 29 |
#define THUMBNAIL_LARGE_PATH THUMBNAIL_PATH"/large" |
| 30 |
#define THUMBNAIL_NORMAL_PATH THUMBNAIL_PATH"/normal" |
| 31 |
#define THUMBNAIL_SMALL_PATH THUMBNAIL_PATH"/small" |
| 32 |
|
| 33 |
inline QByteArray dataToMd5Hex(const QByteArray &data) |
| 34 |
{ |
| 35 |
return QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex(); |
| 36 |
} |
| 37 |
|
| 38 |
#ifndef UT_DThumbnailProviderPrivate |
| 39 |
class DThumbnailProviderPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate |
| 40 |
{ |
| 41 |
public: |
| 42 |
explicit DThumbnailProviderPrivate(DThumbnailProvider *qq); |
| 43 |
|
| 44 |
void init(); |
| 45 |
|
| 46 |
QString sizeToFilePath(DThumbnailProvider::Size size) const; |
| 47 |
|
| 48 |
QString errorString; |
| 49 |
// MAX |
| 50 |
qint64 defaultSizeLimit = INT64_MAX; |
| 51 |
QHash<QMimeType, qint64> sizeLimitHash; |
| 52 |
QMimeDatabase mimeDatabase; |
| 53 |
|
| 54 |
static QSet<QString> hasThumbnailMimeHash; |
| 55 |
|
| 56 |
struct ProduceInfo |
| 57 |
{ |
| 58 |
QFileInfo fileInfo; |
| 59 |
DThumbnailProvider::Size size; |
| 60 |
DThumbnailProvider::CallBack callback; |
| 61 |
}; |
| 62 |
|
| 63 |
QQueue<ProduceInfo> produceQueue; |
| 64 |
QSet<QPair<QString, DThumbnailProvider::Size>> discardedProduceInfos; |
| 65 |
|
| 66 |
bool running = true; |
| 67 |
|
| 68 |
QWaitCondition waitCondition; |
| 69 |
QReadWriteLock dataReadWriteLock; |
| 70 |
|
| 71 |
D_DECLARE_PUBLIC(DThumbnailProvider) |
| 72 |
}; |
| 73 |
#endif |
| 74 |
|
| 75 |
QSet<QString> DThumbnailProviderPrivate::hasThumbnailMimeHash; |
| 76 |
|
| 77 |
DThumbnailProviderPrivate::DThumbnailProviderPrivate(DThumbnailProvider *qq) |
| 78 |
: DObjectPrivate(qq) |
| 79 |
{ |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
void DThumbnailProviderPrivate::init() |
| 84 |
{ |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
QString DThumbnailProviderPrivate::sizeToFilePath(DThumbnailProvider::Size size) const |
| 89 |
{ |
| 90 |
switch (size) |
| 91 |
{ |
| 92 |
case DThumbnailProvider::Small: |
| 93 |
return THUMBNAIL_SMALL_PATH; |
| 94 |
case DThumbnailProvider::Normal: |
| 95 |
return THUMBNAIL_NORMAL_PATH; |
| 96 |
case DThumbnailProvider::Large: |
| 97 |
return THUMBNAIL_LARGE_PATH; |
| 98 |
} |
| 99 |
|
| 100 |
return QString(); |
| 101 |
} |
| 102 |
|
| 103 |
class DFileThumbnailProviderPrivate : public DThumbnailProvider {}; |
| 104 |
Q_GLOBAL_STATIC(DFileThumbnailProviderPrivate, ftpGlobal) |
| 105 |
|
| 106 |
DThumbnailProvider *DThumbnailProvider::instance() |
| 107 |
{ |
| 108 |
return ftpGlobal; |
| 109 |
} |
| 110 |
|
| 111 |
/*! |
| 112 |
\brief DThumbnailProvider::hasThumbnail缩略图是否存在 |
| 113 |
\a info 文件信息 |
| 114 |
\return true 存在 false 不存在 |
| 115 |
*/ |
| 116 |
bool DThumbnailProvider::hasThumbnail(const QFileInfo &info) const |
| 117 |
{ |
| 118 |
Q_D(const DThumbnailProvider); |
| 119 |
|
| 120 |
if (!info.isReadable() || !info.isFile()) |
| 121 |
{ |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
qint64 fileSize = info.size(); |
| 126 |
|
| 127 |
if (fileSize <= 0) |
| 128 |
{ |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
const QMimeType &mime = d->mimeDatabase.mimeTypeForFile(info); |
| 133 |
|
| 134 |
if (fileSize > sizeLimit(mime)) |
| 135 |
{ |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
return hasThumbnail(mime); |
| 140 |
} |
| 141 |
|
| 142 |
bool DThumbnailProvider::hasThumbnail(const QMimeType &mimeType) const |
| 143 |
{ |
| 144 |
const QString &mime = mimeType.name(); |
| 145 |
|
| 146 |
if (DThumbnailProviderPrivate::hasThumbnailMimeHash.isEmpty()) |
| 147 |
{ |
| 148 |
const QList<QByteArray> &mimeTypes = QImageReader::supportedMimeTypes(); |
| 149 |
|
| 150 |
if (mimeTypes.isEmpty()) |
| 151 |
{ |
| 152 |
DThumbnailProviderPrivate::hasThumbnailMimeHash.insert(""); |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
DThumbnailProviderPrivate::hasThumbnailMimeHash.reserve(mimeTypes.size()); |
| 158 |
|
| 159 |
for (const QByteArray &t : mimeTypes) |
| 160 |
{ |
| 161 |
DThumbnailProviderPrivate::hasThumbnailMimeHash.insert(QString::fromLocal8Bit(t)); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return DThumbnailProviderPrivate::hasThumbnailMimeHash.contains(mime); |
| 166 |
} |
| 167 |
|
| 168 |
/*! |
| 169 |
\brief DThumbnailProvider::thumbnailFilePath返回文件缩略图文件路径 |
| 170 |
\a info 文件信息 |
| 171 |
\a size 图片大小 |
| 172 |
\return 路径信息 |
| 173 |
*/ |
| 174 |
QString DThumbnailProvider::thumbnailFilePath(const QFileInfo &info, Size size) const |
| 175 |
{ |
| 176 |
Q_D(const DThumbnailProvider); |
| 177 |
|
| 178 |
const QString &absolutePath = info.absolutePath(); |
| 179 |
const QString &absoluteFilePath = info.absoluteFilePath(); |
| 180 |
|
| 181 |
if (absolutePath == d->sizeToFilePath(Small) |
| 182 |
|| absolutePath == d->sizeToFilePath(Normal) |
| 183 |
|| absolutePath == d->sizeToFilePath(Large) |
| 184 |
|| absolutePath == THUMBNAIL_FAIL_PATH) |
| 185 |
{ |
| 186 |
return absoluteFilePath; |
| 187 |
} |
| 188 |
|
| 189 |
const QString thumbnailName = dataToMd5Hex(QUrl::fromLocalFile(absoluteFilePath).toString(QUrl::FullyEncoded).toLocal8Bit()) + FORMAT; |
| 190 |
QString thumbnail = d->sizeToFilePath(size) + QDir::separator() + thumbnailName; |
| 191 |
|
| 192 |
if (!QFile::exists(thumbnail)) |
| 193 |
{ |
| 194 |
return QString(); |
| 195 |
} |
| 196 |
|
| 197 |
QImage image(thumbnail); |
| 198 |
|
| 199 |
if (image.text(QT_STRINGIFY(Thumb::MTime)) != info.lastModified().toString(Qt::ISODate)) |
| 200 |
{ |
| 201 |
QFile::remove(thumbnail); |
| 202 |
|
| 203 |
Q_EMIT thumbnailChanged(absoluteFilePath, QString()); |
| 204 |
|
| 205 |
return QString(); |
| 206 |
} |
| 207 |
|
| 208 |
return thumbnail; |
| 209 |
} |
| 210 |
|
| 211 |
/*! |
| 212 |
\brief DThumbnailProvider::createThumbnail创建缩略图 |
| 213 |
\a info 文件信息 |
| 214 |
\a size 图片大小 |
| 215 |
\return 成功返回绝对路径信息,失败则返回空 |
| 216 |
*/ |
| 217 |
QString DThumbnailProvider::createThumbnail(const QFileInfo &info, DThumbnailProvider::Size size) |
| 218 |
{ |
| 219 |
Q_D(DThumbnailProvider); |
| 220 |
|
| 221 |
d->errorString.clear(); |
| 222 |
|
| 223 |
const QString &absolutePath = info.absolutePath(); |
| 224 |
const QString &absoluteFilePath = info.absoluteFilePath(); |
| 225 |
|
| 226 |
if (absolutePath == d->sizeToFilePath(Small) |
| 227 |
|| absolutePath == d->sizeToFilePath(Normal) |
| 228 |
|| absolutePath == d->sizeToFilePath(Large) |
| 229 |
|| absolutePath == THUMBNAIL_FAIL_PATH) |
| 230 |
{ |
| 231 |
return absoluteFilePath; |
| 232 |
} |
| 233 |
|
| 234 |
if (!hasThumbnail(info)) |
| 235 |
{ |
| 236 |
d->errorString = QStringLiteral("This file has not support thumbnail: ") + absoluteFilePath; |
| 237 |
|
| 238 |
//!Warnning: Do not store thumbnails to the fail path |
| 239 |
return QString(); |
| 240 |
} |
| 241 |
|
| 242 |
const QString fileUrl = QUrl::fromLocalFile(absoluteFilePath).toString(QUrl::FullyEncoded); |
| 243 |
const QString thumbnailName = dataToMd5Hex(fileUrl.toLocal8Bit()) + FORMAT; |
| 244 |
|
| 245 |
// the file is in fail path |
| 246 |
QString thumbnail = THUMBNAIL_FAIL_PATH + QDir::separator() + thumbnailName; |
| 247 |
|
| 248 |
if (QFile::exists(thumbnail)) |
| 249 |
{ |
| 250 |
QImage image(thumbnail); |
| 251 |
|
| 252 |
if (image.text(QT_STRINGIFY(Thumb::MTime)) != info.lastModified().toString(Qt::ISODate)) |
| 253 |
{ |
| 254 |
QFile::remove(thumbnail); |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
return QString(); |
| 259 |
} |
| 260 |
}// end |
| 261 |
|
| 262 |
QScopedPointer<QImage> image(new QImage(QSize(size, size), QImage::Format_ARGB32_Premultiplied)); |
| 263 |
QImageReader reader(absoluteFilePath); |
| 264 |
|
| 265 |
if (!reader.canRead()) |
| 266 |
{ |
| 267 |
reader.setFormat(d->mimeDatabase.mimeTypeForFile(info).name().toLocal8Bit()); |
| 268 |
|
| 269 |
if (!reader.canRead()) |
| 270 |
{ |
| 271 |
d->errorString = reader.errorString(); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if (d->errorString.isEmpty()) |
| 276 |
{ |
| 277 |
const QSize &imageSize = reader.size(); |
| 278 |
|
| 279 |
if (imageSize.isValid()) |
| 280 |
{ |
| 281 |
if (imageSize.width() >= size || imageSize.height() >= size) |
| 282 |
{ |
| 283 |
reader.setScaledSize(reader.size().scaled(size, size, Qt::KeepAspectRatio)); |
| 284 |
} |
| 285 |
|
| 286 |
if (!reader.read(image.data())) |
| 287 |
{ |
| 288 |
d->errorString = reader.errorString(); |
| 289 |
} |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
d->errorString = "Fail to read image file attribute data:" + info.absoluteFilePath(); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// successful |
| 298 |
if (d->errorString.isEmpty()) |
| 299 |
{ |
| 300 |
thumbnail = d->sizeToFilePath(size) + QDir::separator() + thumbnailName; |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
//fail |
| 305 |
image.reset(new QImage(1, 1, QImage::Format_Mono)); |
| 306 |
} |
| 307 |
|
| 308 |
image->setText(QT_STRINGIFY(Thumb::URL), fileUrl); |
| 309 |
image->setText(QT_STRINGIFY(Thumb::MTime), info.lastModified().toString(Qt::ISODate)); |
| 310 |
|
| 311 |
// create path |
| 312 |
QFileInfo(thumbnail).absoluteDir().mkpath("."); |
| 313 |
|
| 314 |
if (!image->save(thumbnail, Q_NULLPTR, 80)) |
| 315 |
{ |
| 316 |
d->errorString = QStringLiteral("Can not save image to ") + thumbnail; |
| 317 |
} |
| 318 |
|
| 319 |
if (d->errorString.isEmpty()) |
| 320 |
{ |
| 321 |
Q_EMIT createThumbnailFinished(absoluteFilePath, thumbnail); |
| 322 |
Q_EMIT thumbnailChanged(absoluteFilePath, thumbnail); |
| 323 |
|
| 324 |
return thumbnail; |
| 325 |
} |
| 326 |
|
| 327 |
// fail |
| 328 |
Q_EMIT createThumbnailFailed(absoluteFilePath); |
| 329 |
|
| 330 |
return QString(); |
| 331 |
} |
| 332 |
|
| 333 |
void DThumbnailProvider::appendToProduceQueue(const QFileInfo &info, DThumbnailProvider::Size size, DThumbnailProvider::CallBack callback) |
| 334 |
{ |
| 335 |
DThumbnailProviderPrivate::ProduceInfo produceInfo; |
| 336 |
|
| 337 |
produceInfo.fileInfo = info; |
| 338 |
produceInfo.size = size; |
| 339 |
produceInfo.callback = callback; |
| 340 |
|
| 341 |
Q_D(DThumbnailProvider); |
| 342 |
|
| 343 |
if (isRunning()) |
| 344 |
{ |
| 345 |
QWriteLocker locker(&d->dataReadWriteLock); |
| 346 |
d->produceQueue.append(std::move(produceInfo)); |
| 347 |
locker.unlock(); |
| 348 |
d->waitCondition.wakeAll(); |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
d->produceQueue.append(std::move(produceInfo)); |
| 353 |
start(); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/*! |
| 358 |
\brief DThumbnailProvider::removeInProduceQueue将缩略图从列表中删除 |
| 359 |
\a info 缩略图文件 |
| 360 |
\a size 缩略图大小 |
| 361 |
*/ |
| 362 |
void DThumbnailProvider::removeInProduceQueue(const QFileInfo &info, DThumbnailProvider::Size size) |
| 363 |
{ |
| 364 |
Q_D(DThumbnailProvider); |
| 365 |
|
| 366 |
if (isRunning()) |
| 367 |
{ |
| 368 |
QWriteLocker locker(&d->dataReadWriteLock); |
| 369 |
Q_UNUSED(locker) |
| 370 |
} |
| 371 |
|
| 372 |
d->discardedProduceInfos.insert(qMakePair(info.absoluteFilePath(), size)); |
| 373 |
} |
| 374 |
|
| 375 |
/*! |
| 376 |
\brief DThumbnailProvider::errorString返回错误信息 |
| 377 |
\return 错误信息 |
| 378 |
*/ |
| 379 |
QString DThumbnailProvider::errorString() const |
| 380 |
{ |
| 381 |
Q_D(const DThumbnailProvider); |
| 382 |
|
| 383 |
return d->errorString; |
| 384 |
} |
| 385 |
|
| 386 |
/*! |
| 387 |
\brief DThumbnailProvider::defaultSizeLimit返回缩略图默认大小 |
| 388 |
\return 默认的大小 |
| 389 |
*/ |
| 390 |
qint64 DThumbnailProvider::defaultSizeLimit() const |
| 391 |
{ |
| 392 |
Q_D(const DThumbnailProvider); |
| 393 |
|
| 394 |
return d->defaultSizeLimit; |
| 395 |
} |
| 396 |
|
| 397 |
/*! |
| 398 |
\brief DThumbnailProvider::setDefaultSizeLimit设置缩略图的默认大小 |
| 399 |
\a size 大小 |
| 400 |
*/ |
| 401 |
void DThumbnailProvider::setDefaultSizeLimit(qint64 size) |
| 402 |
{ |
| 403 |
Q_D(DThumbnailProvider); |
| 404 |
|
| 405 |
d->defaultSizeLimit = size; |
| 406 |
} |
| 407 |
|
| 408 |
/*! |
| 409 |
\brief DThumbnailProvider::sizeLimit 返回文件大小 |
| 410 |
\a mimeType 由MIME类型字符串表示的文件或数据类型 |
| 411 |
\return |
| 412 |
*/ |
| 413 |
qint64 DThumbnailProvider::sizeLimit(const QMimeType &mimeType) const |
| 414 |
{ |
| 415 |
Q_D(const DThumbnailProvider); |
| 416 |
|
| 417 |
return d->sizeLimitHash.value(mimeType, d->defaultSizeLimit); |
| 418 |
} |
| 419 |
|
| 420 |
/*! |
| 421 |
\brief DThumbnailProvider::setSizeLimit 设置文件的大小 |
| 422 |
\a mimeType 由MIME类型字符串表示的文件或数据类型 |
| 423 |
\a size 范围 |
| 424 |
*/ |
| 425 |
void DThumbnailProvider::setSizeLimit(const QMimeType &mimeType, qint64 size) |
| 426 |
{ |
| 427 |
Q_D(DThumbnailProvider); |
| 428 |
|
| 429 |
d->sizeLimitHash[mimeType] = size; |
| 430 |
} |
| 431 |
|
| 432 |
/*! |
| 433 |
\class Dtk::Gui::DThumbnailProvider |
| 434 |
\inmodule dtkgui |
| 435 |
|
| 436 |
\brief 缩略图生成类. |
| 437 |
\note 缩略图创建失败 |
| 438 |
该文件格式未知,无法由程序加载。 |
| 439 |
文件格式是已知的,但是文件已被损坏,因此无法读取。 |
| 440 |
由于文件很大,缩略图的生成将花费很长时间 |
| 441 |
*/ |
| 442 |
|
| 443 |
DThumbnailProvider::DThumbnailProvider(QObject *parent) |
| 444 |
: QThread(parent) |
| 445 |
, DObject(*new DThumbnailProviderPrivate(this)) |
| 446 |
{ |
| 447 |
d_func()->init(); |
| 448 |
} |
| 449 |
|
| 450 |
DThumbnailProvider::~DThumbnailProvider() |
| 451 |
{ |
| 452 |
Q_D(DThumbnailProvider); |
| 453 |
|
| 454 |
d->running = false; |
| 455 |
d->waitCondition.wakeAll(); |
| 456 |
wait(); |
| 457 |
} |
| 458 |
|
| 459 |
void DThumbnailProvider::run() |
| 460 |
{ |
| 461 |
Q_D(DThumbnailProvider); |
| 462 |
|
| 463 |
Q_FOREVER |
| 464 |
{ |
| 465 |
QWriteLocker locker(&d->dataReadWriteLock); |
| 466 |
|
| 467 |
if (d->produceQueue.isEmpty()) |
| 468 |
{ |
| 469 |
d->waitCondition.wait(&d->dataReadWriteLock); |
| 470 |
} |
| 471 |
|
| 472 |
if (!d->running) |
| 473 |
{ |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
const DThumbnailProviderPrivate::ProduceInfo &task = d->produceQueue.dequeue(); |
| 478 |
const QPair<QString, DThumbnailProvider::Size> &tmpKey = qMakePair(task.fileInfo.absoluteFilePath(), task.size); |
| 479 |
|
| 480 |
if (d->discardedProduceInfos.contains(tmpKey)) |
| 481 |
{ |
| 482 |
d->discardedProduceInfos.remove(tmpKey); |
| 483 |
locker.unlock(); |
| 484 |
continue; |
| 485 |
} |
| 486 |
|
| 487 |
locker.unlock(); |
| 488 |
|
| 489 |
const QString &thumbnail = createThumbnail(task.fileInfo, task.size); |
| 490 |
|
| 491 |
if (task.callback) |
| 492 |
{ |
| 493 |
task.callback(thumbnail); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
DGUI_END_NAMESPACE |