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
|
#include "pictureCache.h"
#include <Poco/Format.h>
#include <Poco/URIStreamOpener.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/StringTokenizer.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include <Poco/Net/HTTPStreamFactory.h>
#include "plexSdOsd.h"
#include "PVideo.h"
#include "Directory.h"
using Poco::URIStreamOpener;
using Poco::StreamCopier;
using Poco::Path;
using Poco::Exception;
using Poco::Net::HTTPStreamFactory;
cPictureCache::cPictureCache()
{
m_cacheDir = cPlugin::CacheDirectory(PLUGIN_NAME_I18N);
Poco::Path path(m_cacheDir);
Poco::File f(path.toString());
f.createDirectories();
HTTPStreamFactory::registerFactory();
}
void cPictureCache::Action()
{
while(Running()) {
while (m_qImagesToLoad.size() > 0) {
//LOCK_THREAD;
CacheInfo info = m_qImagesToLoad.front();
m_qImagesToLoad.pop_front();
std::string transcodeUri = TranscodeUri(info.uri, info.width, info.height);
std::string file = FileName(info.uri, info.width);
if(!Cached(info.uri, info.width)) {
auto stream = DownloadFile(transcodeUri);
if(stream) {
SaveFileToDisk(stream, file);
cMutexLock MutexLock(&cPlexSdOsd::RedrawMutex);
if (info.onCached && info.calle && info.calle->IsVisible()) {
info.onCached(info.calle);
}
}
}
cCondWait::SleepMs(5);
}
cCondWait::SleepMs(100);
}
}
std::shared_ptr<std::istream> cPictureCache::DownloadFile(std::string uri)
{
try {
std::shared_ptr<std::istream> pStream(URIStreamOpener::defaultOpener().open(uri));
return pStream;
} catch (Poco::Exception &exc) {
return NULL;
}
}
void cPictureCache::SaveFileToDisk(std::shared_ptr<std::istream> file, std::string fileName)
{
try {
Poco::Path p(fileName);
Poco::File f(p.makeParent().toString());
f.createDirectories();
std::ofstream outFile;
outFile.open(fileName);
Poco::StreamCopier::copyStream(*file, outFile);
outFile.close();
} catch (Poco::Exception &exc) {
std::cout << "SaveFile Error: " << exc.displayText() << std::endl;
}
}
std::string cPictureCache::FileName(std::string uri, int width)
{
Poco::URI u(uri);
std::string file = Poco::format("%s_%d", u.getPath(), width);
Poco::Path path(m_cacheDir);
path.append(file);
return path.toString();
}
std::string cPictureCache::TranscodeUri(std::string uri, int width, int height)
{
std::string escapedUri;
Poco::URI::encode(uri, " !\"#$%&'()*+,/:;=?@[]", escapedUri);
Poco::URI u(uri);
int port = u.getPort();
std::string tUri = Poco::format("http://%s:%d/photo/:/transcode?width=%d&height=%d&url=%s", u.getHost(), port, width, height, escapedUri);
return tUri;
}
bool cPictureCache::Cached(std::string uri, int width)
{
return Poco::File(FileName(uri, width)).exists();
}
std::string cPictureCache::GetPath(std::string uri, int width, int height, bool& cached, std::function<void(cGridElement*)> OnCached, cGridElement* calle)
{
cached = Cached(uri, width);
std::string file = FileName(uri, width);
if(cached) {
return file;
} else {
//LOCK_THREAD;
CacheInfo info(uri, width, height, OnCached, calle);
m_qImagesToLoad.push_back(info);
}
return file;
}
void cPictureCache::Stop()
{
Cancel();
}
void cPictureCache::Remove(cGridElement* element)
{
if(!element) return;
if (auto video = dynamic_cast<plexclient::Video*>(element)) {
Remove(video->ThumbUri());
Remove(video->ArtUri());
}
}
void cPictureCache::Remove(std::string uri)
{
for(std::deque<CacheInfo>::iterator it = m_qImagesToLoad.begin() ; it != m_qImagesToLoad.end(); ++it) {
if(it->uri == uri) {
m_qImagesToLoad.erase(it);
return;
}
}
}
|