/
fileutils.cpp
238 lines (195 loc) · 7.11 KB
/
fileutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
fileutils.cpp
---------------------
begin : 29.02.2020
copyright : (C) 2020 by david
email : david at opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "fileutils.h"
#include "gnsspositioninformation.h"
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFileInfo>
#include <QImage>
#include <QImageReader>
#include <QMimeDatabase>
#include <qgis.h>
#include <qgsexiftools.h>
#include <qgsfileutils.h>
FileUtils::FileUtils( QObject *parent )
: QObject( parent )
{
}
QString FileUtils::mimeTypeName( const QString &filePath )
{
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForFile( filePath );
return mimeType.name();
}
bool FileUtils::isImageMimeTypeSupported( const QString &mimeType )
{
return QImageReader::supportedMimeTypes().contains( mimeType.toLatin1() );
}
QString FileUtils::fileName( const QString &filePath )
{
QFileInfo fileInfo( filePath );
return fileInfo.fileName();
}
QString FileUtils::fileSuffix( const QString &filePath )
{
QFileInfo fileInfo( filePath );
return fileInfo.suffix();
}
bool FileUtils::fileExists( const QString &filePath )
{
QFileInfo fileInfo( filePath );
return ( fileInfo.exists() && fileInfo.isFile() );
}
QString FileUtils::representFileSize( qint64 bytes )
{
return QgsFileUtils::representFileSize( bytes );
}
bool FileUtils::copyRecursively( const QString &sourceFolder, const QString &destFolder, QgsFeedback *feedback, bool wipeDestFolder )
{
// Remove the destination folder and its content if it already exists
if ( wipeDestFolder )
{
QDir destDir( destFolder );
if ( destDir.exists() )
{
bool success = destDir.removeRecursively();
if ( !success )
{
qDebug() << QStringLiteral( "Failed to recursively delete directory %1" ).arg( destFolder );
return false;
}
}
}
QList<QPair<QString, QString>> mapping;
int fileCount = copyRecursivelyPrepare( sourceFolder, destFolder, mapping );
int current = 0;
for ( QPair<QString, QString> srcDestFilePair : std::as_const( mapping ) )
{
QString srcName = srcDestFilePair.first;
QString destName = srcDestFilePair.second;
QFileInfo destInfo( destName );
if ( QFileInfo( srcName ).isDir() )
continue;
QDir destDir( destInfo.absoluteDir() );
if ( !destDir.exists() )
{
destDir.mkpath( destDir.path() );
}
if ( QFile::exists( destName ) )
QFile::remove( destName );
bool success = QFile::copy( srcName, destName );
if ( !success )
{
qDebug() << QStringLiteral( "Failed to write file %1" ).arg( destName );
return false;
}
QFile( destName ).setPermissions( QFileDevice::ReadOwner | QFileDevice::WriteOwner );
if ( feedback )
feedback->setProgress( 100 * current / fileCount );
++current;
}
return true;
}
int FileUtils::copyRecursivelyPrepare( const QString &sourceFolder, const QString &destFolder, QList<QPair<QString, QString>> &mapping )
{
QDir sourceDir( sourceFolder );
if ( !sourceDir.exists() )
return 0;
int count = 0;
QDirIterator dirIt( sourceDir, QDirIterator::Subdirectories );
const qsizetype sfLength = sourceFolder.length();
while ( dirIt.hasNext() )
{
QString filePath = dirIt.next();
const QString relPath = filePath.mid( sfLength );
if ( relPath.endsWith( QLatin1String( "/." ) ) || relPath.endsWith( QLatin1String( "/.." ) ) )
continue;
QString srcName = QDir::cleanPath( sourceFolder + QDir::separator() + relPath );
QString destName = QDir::cleanPath( destFolder + QDir::separator() + relPath );
mapping.append( qMakePair( srcName, destName ) );
count += 1;
}
return count;
}
QByteArray FileUtils::fileChecksum( const QString &fileName, const QCryptographicHash::Algorithm hashAlgorithm )
{
QFile f( fileName );
if ( !f.open( QFile::ReadOnly ) )
return QByteArray();
QCryptographicHash hash( hashAlgorithm );
if ( hash.addData( &f ) )
return hash.result();
return QByteArray();
}
void FileUtils::restrictImageSize( const QString &imagePath, int maximumWidthHeight )
{
if ( !QFileInfo::exists( imagePath ) )
{
return;
}
QVariantMap metadata = QgsExifTools::readTags( imagePath );
QImage img( imagePath );
if ( !img.isNull() && ( img.width() > maximumWidthHeight || img.height() > maximumWidthHeight ) )
{
QImage scaledImage = img.width() > img.height()
? img.scaledToWidth( maximumWidthHeight, Qt::SmoothTransformation )
: img.scaledToHeight( maximumWidthHeight, Qt::SmoothTransformation );
scaledImage.save( imagePath );
for ( const QString key : metadata.keys() )
{
QgsExifTools::tagImage( imagePath, key, metadata[key] );
}
}
}
void FileUtils::addImageMetadata( const QString &imagePath, const GnssPositionInformation &positionInformation )
{
if ( !QFileInfo::exists( imagePath ) )
{
return;
}
QVariantMap metadata;
if ( positionInformation.latitudeValid() && positionInformation.longitudeValid() )
{
metadata["Exif.GPSInfo.GPSLatitude"] = std::abs( positionInformation.latitude() );
metadata["Exif.GPSInfo.GPSLatitudeRef"] = positionInformation.latitude() >= 0 ? "N" : "S";
metadata["Exif.GPSInfo.GPSLongitude"] = std::abs( positionInformation.longitude() );
metadata["Exif.GPSInfo.GPSLongitudeRef"] = positionInformation.longitude() >= 0 ? "E" : "W";
if ( positionInformation.elevationValid() )
{
metadata["Exif.GPSInfo.GPSAltitude"] = std::abs( positionInformation.elevation() );
metadata["Exif.GPSInfo.GPSAltitudeRef"] = positionInformation.elevation() >= 0 ? "1" : "0";
}
}
if ( positionInformation.orientationValid() )
{
metadata["Exif.GPSInfo.GPSImgDirection"] = positionInformation.orientation();
metadata["Exif.GPSInfo.GPSImgDirectionRef"] = "M";
}
if ( positionInformation.speedValid() )
{
metadata["Exif.GPSInfo.GPSSpeed"] = positionInformation.speed();
metadata["Exif.GPSInfo.GPSSpeedRef"] = "K";
}
metadata["Exif.GPSInfo.GPSDateStamp"] = positionInformation.utcDateTime().date();
metadata["Exif.GPSInfo.GPSTimeStamp"] = positionInformation.utcDateTime().time();
metadata["Exif.GPSInfo.GPSSatellites"] = QString::number( positionInformation.satellitesUsed() ).rightJustified( 2, '0' );
metadata["Exif.Image.Make"] = QStringLiteral( "QField" );
metadata["Xmp.tiff.Make"] = QStringLiteral( "QField" );
for ( const QString key : metadata.keys() )
{
QgsExifTools::tagImage( imagePath, key, metadata[key] );
}
}