forked from sialan-labs/kaqaz
-
Notifications
You must be signed in to change notification settings - Fork 12
/
repository.cpp
146 lines (117 loc) · 3.48 KB
/
repository.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
/*
Copyright (C) 2014 Aseman
http://aseman.co
Papyrus 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 3 of the License, or
(at your option) any later version.
Papyrus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define NORMALIZE_PATH( PATH ) \
while( PATH.left(7) == "file://" ) \
PATH = PATH.mid(7);
#include "repository.h"
#include "papyrus.h"
#include <QDir>
#include <QCryptographicHash>
#include <QFileInfo>
#include <QFile>
#include <QDateTime>
#include <QDate>
#include <QImage>
#include <QImageReader>
#include <QImageWriter>
#include <QDebug>
class RepositoryPrivate
{
public:
Papyrus *papyrus;
};
Repository::Repository(QObject *parent) :
QObject(parent)
{
p = new RepositoryPrivate;
p->papyrus = Papyrus::instance();
}
QString Repository::insert(QString path)
{
NORMALIZE_PATH(path)
QString id = generateID(path);
QString dest = p->papyrus->repositoryPath() + "/" + id;
if( QFile::exists(dest) )
return id;
QFile::copy( path, dest );
return id;
}
QString Repository::getPath(const QString &id)
{
return p->papyrus->repositoryPath() + "/" + id;
}
QString Repository::repositoryPath()
{
return p->papyrus->repositoryPath();
}
QString Repository::copyImageToRepository(QString path, int size)
{
NORMALIZE_PATH(path)
const QString & id = generateID( path );
const QString & dest_path = getPath( id );
if( QFile::exists(dest_path) )
return id;
QImageReader reader(path);
QSize dest_size = reader.size();
if( dest_size.width() > dest_size.height() )
{
if( dest_size.width() < size )
size = dest_size.width();
dest_size.setHeight( size*dest_size.height()/dest_size.width() );
dest_size.setWidth( size );
}
else
{
if( dest_size.height() < size )
size = dest_size.height();
dest_size.setWidth( size*dest_size.width()/dest_size.height() );
dest_size.setHeight( size );
}
QImage img = reader.read();
img = img.scaled( dest_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
QImageWriter writer( dest_path );
writer.write( img );
return id;
}
QString Repository::copyAudioToRepository(QString path)
{
NORMALIZE_PATH(path)
return insert(path);
}
void Repository::deleteFile(const QString &f)
{
const QString & path = getPath(f);
QFile file(path);
file.remove();
if( !file.open(QFile::WriteOnly) )
return;
file.write("DELETED");
file.flush();
file.close();
}
QString Repository::generateID(QString path)
{
NORMALIZE_PATH(path)
QFileInfo file(path);
QString hidden_text = path + " " + QString::number(file.size()) + " " +
file.created().toString("yyyy/MM/dd hh:mm:ss:zzz") + " " +
file.lastModified().toString("yyyy/MM/dd hh:mm:ss:zzz");
QString id = QCryptographicHash::hash(hidden_text.toUtf8(),QCryptographicHash::Md5).toHex() + "." + file.suffix();
return id;
}
Repository::~Repository()
{
delete p;
}