-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilereader.cpp
More file actions
82 lines (68 loc) · 2.13 KB
/
filereader.cpp
File metadata and controls
82 lines (68 loc) · 2.13 KB
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
#include "filereader.h"
#include <QFile>
#include <QTextStream>
#include <QUrl>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QDebug>
FileReader::FileReader(QObject *parent) : QObject(parent) {}
QString FileReader::readFile(const QString &fileUrl) {
QString localPath;
// Handle both file:// URLs and direct paths
if (fileUrl.startsWith("file:///")) {
localPath = QUrl(fileUrl).toLocalFile();
} else if (fileUrl.startsWith("file://")) {
localPath = QUrl(fileUrl).toLocalFile();
} else if (fileUrl.startsWith("/")) {
localPath = fileUrl;
} else {
// Assume it's a relative path, convert to file URL first
QUrl url = QUrl::fromLocalFile(fileUrl);
localPath = url.toLocalFile();
}
QFile file(localPath);
if (!file.exists()) {
QString error = "File not found: " + localPath;
emit errorOccurred(error);
return "";
}
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString error = "Failed to open: " + file.errorString();
emit errorOccurred(error);
return "";
}
QTextStream in(&file);
in.setAutoDetectUnicode(true);
QString content = in.readAll();
file.close();
if (content.isEmpty()) {
qWarning() << "Warning: File is empty:" << localPath;
}
return content;
}
QString FileReader::getFileInfo(const QString &fileUrl) {
QString localPath;
if (fileUrl.startsWith("file:///")) {
localPath = QUrl(fileUrl).toLocalFile();
} else if (fileUrl.startsWith("file://")) {
localPath = QUrl(fileUrl).toLocalFile();
} else {
localPath = fileUrl;
}
QFileInfo info(localPath);
if (!info.exists()) {
return "File not found";
}
QString suffix = info.suffix().toUpper();
if (suffix.isEmpty()) {
suffix = "Unknown";
}
qint64 sizeKB = info.size() / 1024;
if (sizeKB == 0 && info.size() > 0) {
sizeKB = 1; // Show at least 1 KB for non-empty files
}
return QString("%1 | %2 KB | %3")
.arg(suffix)
.arg(sizeKB)
.arg(info.lastModified().toString("yyyy-MM-dd"));
}