summaryrefslogtreecommitdiffstats
path: root/helper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'helper.cpp')
-rw-r--r--helper.cpp43
1 files changed, 24 insertions, 19 deletions
diff --git a/helper.cpp b/helper.cpp
index b1f277b..02b3fac 100644
--- a/helper.cpp
+++ b/helper.cpp
@@ -41,7 +41,7 @@ namespace Helper {
magic_t mc = magic_open(MAGIC_MIME_TYPE);
QByteArray name = path.toUtf8();
if(mc){
- magic_load(mc, 0);
+ magic_load(mc, nullptr);
const char* magic_c = magic_file(mc, name.constData());
retval = QString(magic_c);
magic_close(mc);
@@ -49,7 +49,7 @@ namespace Helper {
if(retval.toLower().startsWith("application/octet-stream")){
magic_t mc = magic_open(MAGIC_NONE);
if(mc){
- magic_load(mc, 0);
+ magic_load(mc, nullptr);
const char* magic_c = magic_file(mc, name.constData());
QString desc(magic_c);
magic_close(mc);
@@ -67,29 +67,34 @@ namespace Helper {
return QString();
}
+ const int blockSize = 4096;
+ const int bigBlockSize = 512;
QString retval;
QCryptographicHash h(QCryptographicHash::Md5);
QFile file(path);
file.open(QIODevice::ReadOnly);
- qint64 read = 0;
+ int read = 0;
+ // if the file is smaller than 5MB hash it entirely
if(info.size() < (5 * 1024 * 1024)){
- QByteArray data(4096, '\0');
+ QByteArray data(blockSize, '\0');
do {
- read = file.read(data.data(), 4096);
+ //read can never be bigger than blockSize, that 4k for now...
+ read = static_cast<int>(file.read(data.data(), blockSize));
if(read > 0){
h.addData(data.data(), read);
}
- } while (read == 4096);
+ } while (read == blockSize);
QByteArray res = h.result();
retval = res.toHex().toLower();
+ // otherwise hash only parts for performance
}else{
- QByteArray data(512, '\0');
- int offset = info.size() / 3;
+ QByteArray data(bigBlockSize, '\0');
+ auto offset = info.size() / 3;
file.seek(offset);
- int numBytes = 512 * 1024;
+ int numBytes = bigBlockSize * 1024;
int readBytes = 0;
do {
- read = file.read(data.data(), 512);
+ read = static_cast<int>(file.read(data.data(), bigBlockSize));
if(read > 0){
readBytes += read;
}else{
@@ -192,9 +197,9 @@ namespace Helper {
}
const QString durationFromSecs(qint64 secs){
- int minutes = secs / 60;
- int hours = 0;
- int seconds = 0;
+ qint64 minutes = secs / 60;
+ qint64 hours = 0;
+ qint64 seconds = 0;
if(minutes > 60){
hours = minutes / 60;
minutes -= hours * 60;
@@ -202,7 +207,7 @@ namespace Helper {
seconds = secs - hours * 60 * 60 - minutes * 60;
seconds = (seconds > 60) ? 59 : seconds;
QByteArray retval(10, '\0');
- ::snprintf(retval.data(), 9, "%.2d:%.2d:%.2d", hours, minutes, seconds);
+ ::snprintf(retval.data(), 9, "%lld::%lld:%lld", hours, minutes, seconds);
return retval;
}
@@ -221,7 +226,7 @@ namespace Helper {
const QStringList toStringList(const QList<QVariant> &list){
QStringList retval;
- foreach(QVariant v, list){
+ for(QVariant v : list){
retval << v.toString();
}
return retval;
@@ -273,8 +278,8 @@ namespace Helper {
QPixmap preview(const QString &path){
QVariantMap m = ffmpegData(path);
- int secs = m.value("duration").toDouble();
- int interval = secs / 4;
+ auto secs = m.value("duration").toInt();
+ auto interval = secs / 4;
QImage retval(640 * 2 + 40, 480 * 2 + 40, QImage::Format_ARGB32);
retval.fill(Qt::transparent);
QPainter p(&retval);
@@ -344,7 +349,7 @@ namespace Helper {
try {
img.write(outFile.fileName().toStdString());
}
- catch(Magick::Exception &e) {
+ catch(Magick::Exception &) {
return retval;
}
outFile.rename(newFn);
@@ -401,7 +406,7 @@ namespace Helper {
Duration::Duration() : mHours(0), mMinutes(0), mSeconds(0) {}
Duration::Duration(qint64 seconds){
- int sec(0), min(0), h(0);
+ qint64 sec(0), min(0), h(0);
// get hours
h = (seconds / 60 / 60);
// remaining minutes