forked from CPP-Final-Project/Chat_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaSaver_Service.cpp
50 lines (39 loc) · 1.51 KB
/
MediaSaver_Service.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
//
// Created by Admin on 6/16/2023.
//
#include "MediaSaver_Service.h"
MediaSaver_Service::MediaSaver_Service(QObject *parent) : QObject(parent) {
}
QSharedPointer<MediaSaver_Service> MediaSaver_Service::getInstance() {
if (!shp_instance)
{
QMutexLocker locker(&mutex);
if (!shp_instance)
{
shp_instance = QSharedPointer<MediaSaver_Service>(new MediaSaver_Service(), &QObject::deleteLater);
//connect(p_instance, &close, p_instance, &safeExit);
}
}
return shp_instance;
}
QFuture<QByteArray> MediaSaver_Service::getMedia(const QString& login, const QUuid& media_id_, const quint32& room_id_) {
return QtConcurrent::run([&]() {
auto file_path = "rooms/" + QString::number(room_id_) + "/media/" + media_id_.toString();
auto byte_array = FileRepository::readFromBinFile(file_path);
return byte_array;
});
}
void MediaSaver_Service::saveMedia(const quint32 &room_id_, const QString &login, const QUuid &id_,
const QByteArray &data_) {
auto future = QtConcurrent::run([&]() {
auto dir_path = "rooms/" + QString::number(room_id_) + "/media/";
if (!QDir(dir_path).exists())
QDir().mkpath(dir_path);
return FileRepository::saveToBinFile(data_, dir_path + id_.toString());
});
future.then([&](bool success){
if(success)
emit savedMedia(id_, room_id_, login, true);
else emit savedMedia(id_, room_id_, login, false);
});
}