Add better logging for file open and write errors

This commit is contained in:
Jonas Kvinge
2021-08-09 23:32:26 +02:00
parent 7d61d8e646
commit f1d3cadb3b
15 changed files with 120 additions and 41 deletions

View File

@@ -188,6 +188,9 @@ AlbumCoverImageResult AlbumCoverChoiceController::LoadImageFromFile(Song *song)
result.cover_url = QUrl::fromLocalFile(cover_file);
}
}
else {
qLog(Error) << "Failed to open cover file" << cover_file << "for reading:" << file.errorString();
}
return result;
@@ -252,9 +255,14 @@ void AlbumCoverChoiceController::SaveCoverToFileManual(const Song &song, const A
if (result.is_jpeg() && fileinfo.completeSuffix().compare("jpg", Qt::CaseInsensitive) == 0) {
QFile file(save_filename);
if (file.open(QIODevice::WriteOnly)) {
file.write(result.image_data);
if (file.write(result.image_data) <= 0) {
qLog(Error) << "Failed writing cover to file" << save_filename << file.errorString();
}
file.close();
}
else {
qLog(Error) << "Failed to open cover file" << save_filename << "for writing:" << file.errorString();
}
}
else {
result.image.save(save_filename);
@@ -596,9 +604,17 @@ QUrl AlbumCoverChoiceController::SaveCoverToFileAutomatic(const Song::Source sou
QUrl cover_url;
if (result.is_jpeg()) {
if (file.open(QIODevice::WriteOnly)) {
if (file.write(result.image_data) > 0) cover_url = QUrl::fromLocalFile(filepath);
if (file.write(result.image_data) > 0) {
cover_url = QUrl::fromLocalFile(filepath);
}
else {
qLog(Error) << "Failed to write cover to file" << file.fileName() << file.errorString();
}
file.close();
}
else {
qLog(Error) << "Failed to open cover file" << file.fileName() << "for writing:" << file.errorString();
}
}
else {
if (result.image.save(filepath, "JPG")) cover_url = QUrl::fromLocalFile(filepath);

View File

@@ -431,34 +431,47 @@ AlbumCoverLoader::TryLoadResult AlbumCoverLoader::TryLoadImage(Task *task) {
if (cover_url.isLocalFile()) {
QFile file(cover_url.toLocalFile());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
if (file.exists()) {
if (file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
qLog(Error) << "Failed to open cover file" << cover_url << "for reading" << file.errorString();
}
}
else {
qLog(Error) << "Failed to open album cover file" << cover_url;
qLog(Error) << "Cover file" << cover_url << "does not exist";
}
}
else if (cover_url.scheme().isEmpty()) { // Assume a local file with no scheme.
QFile file(cover_url.path());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
if (file.exists()) {
if (file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
qLog(Error) << "Failed to open cover file" << cover_url << "for reading" << file.errorString();
}
}
else {
qLog(Error) << "Cover file" << cover_url << "does not exist";
}
}
else if (network_->supportedSchemes().contains(cover_url.scheme())) { // Remote URL
qLog(Debug) << "Loading remote cover from" << cover_url;
@@ -615,7 +628,13 @@ void AlbumCoverLoader::SaveEmbeddedCover(const qint64 id, const QString &song_fi
QFile file(cover_filename);
if (file.size() >= 209715200 || !file.open(QIODevice::ReadOnly)) { // Max 200 MB.
if (file.size() >= 209715200) { // Max 200 MB.
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}
if (!file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Failed to open cover file" << cover_filename << "for reading:" << file.errorString();
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}
@@ -656,7 +675,13 @@ void AlbumCoverLoader::SaveEmbeddedCover(const qint64 id, const QList<QUrl> &url
QFile file(cover_filename);
if (file.size() >= 209715200 || !file.open(QIODevice::ReadOnly)) { // Max 200 MB.
if (file.size() >= 209715200) { // Max 200 MB.
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}
if (!file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Failed to open cover file" << cover_filename << "for reading:" << file.errorString();
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}

View File

@@ -86,11 +86,11 @@ void CurrentAlbumCoverLoader::TempAlbumCoverLoaded(const quint64 id, AlbumCoverL
result.temp_cover_url = QUrl::fromLocalFile(temp_cover_->fileName());
}
else {
qLog(Error) << "Unable to save cover image to" << temp_cover_->fileName();
qLog(Error) << "Failed to save cover image to" << temp_cover_->fileName() << temp_cover_->errorString();
}
}
else {
qLog(Error) << "Unable to open" << temp_cover_->fileName();
qLog(Error) << "Failed to open" << temp_cover_->fileName() << temp_cover_->errorString();
}
}