1 |
douglas |
476 |
// Media Folder |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
553 |
#include <menes/c++/standard.hh> |
8 |
douglas |
478 |
|
9 |
douglas |
549 |
#include <menes/dbi/resultset.hpp> |
10 |
douglas |
489 |
|
11 |
douglas |
516 |
#include "MediaFolder.hpp" |
12 |
douglas |
550 |
#include "Share.hpp" |
13 |
douglas |
516 |
|
14 |
douglas |
555 |
MediaFolder::MediaFolder(const _R<Connector>& connector, const api::Path& path) : connector(connector), path(path) |
15 |
douglas |
478 |
{ |
16 |
douglas |
555 |
_R<dbi::Connection> connection(connector->Connect()); |
17 |
douglas |
541 |
_R<dbi::ResultSet> root_(connection->Parse(_B("SELECT root FROM folders WHERE path = ?"))->Execute(path.GetPath())); |
18 |
douglas |
500 |
|
19 |
|
|
if (root_->MoveNext()) |
20 |
|
|
root = root_->GetString(_B("root")); |
21 |
douglas |
555 |
|
22 |
|
|
connector->Release(connection); |
23 |
douglas |
478 |
} |
24 |
|
|
|
25 |
douglas |
555 |
MediaFolder::MediaFolder(const _R<Connector>& connector, const api::Path& path, const api::Path& root) : connector(connector), path(path), root(root) |
26 |
douglas |
496 |
{ |
27 |
douglas |
555 |
_R<dbi::Connection> connection(connector->Connect()); |
28 |
douglas |
541 |
_R<dbi::ResultSet> path_(connection->Parse(_B("SELECT path FROM folders WHERE path = ?"))->Execute(path.GetPath())); |
29 |
douglas |
496 |
|
30 |
douglas |
500 |
if (path_->MoveNext()) |
31 |
douglas |
498 |
if (path.GetPath() != root.GetPath()) |
32 |
douglas |
500 |
connection->Parse(_B("UPDATE folders SET folder = ?, root = ? WHERE path = ?"))->Execute(path.GetParent().GetPath(), root.GetPath(), path.GetPath()); |
33 |
|
|
else |
34 |
|
|
connection->Parse(_B("UPDATE folders SET folder = NULL, root = ? WHERE path = ?"))->Execute(root.GetPath(), path.GetPath()); |
35 |
|
|
else |
36 |
|
|
if (path.GetPath() != root.GetPath()) |
37 |
douglas |
498 |
connection->Parse(_B("INSERT INTO folders (path, folder, root) VALUES (?, ?, ?)"))->Execute(path.GetPath(), path.GetParent().GetPath(), root.GetPath()); |
38 |
|
|
else |
39 |
|
|
connection->Parse(_B("INSERT INTO folders (path, folder, root) VALUES (?, NULL, ?)"))->Execute(path.GetPath(), root.GetPath()); |
40 |
douglas |
555 |
|
41 |
|
|
connector->Release(connection); |
42 |
douglas |
496 |
} |
43 |
|
|
|
44 |
douglas |
550 |
cse::String MediaFolder::GetName() const |
45 |
|
|
{ |
46 |
|
|
if (path == root && path.GetPath().StartsWithAll(Share::shares.GetPath())) |
47 |
|
|
{ |
48 |
|
|
api::Address address(api::Address::Resolve(path.GetParent().GetName(), _B("6996")).First()); |
49 |
|
|
cse::String host, port; |
50 |
|
|
|
51 |
|
|
address.ToString(host, port, true); |
52 |
|
|
|
53 |
|
|
return _S<ios::String>() << _B("Share from ") << host; |
54 |
|
|
} |
55 |
|
|
else |
56 |
|
|
return path.GetName(); |
57 |
|
|
} |
58 |
|
|
|
59 |
douglas |
554 |
_L<MediaFile> MediaFolder::GetFiles(unsigned page) const |
60 |
douglas |
478 |
{ |
61 |
douglas |
555 |
_R<dbi::Connection> connection(connector->Connect()); |
62 |
douglas |
554 |
_R<dbi::ResultSet> paths(connection->Parse(_B("SELECT path FROM files WHERE live = TRUE AND folder = ? ORDER BY path LIMIT 50 OFFSET ?"))->Execute(path.GetPath(), page * 50)); |
63 |
douglas |
478 |
_L<MediaFile> files; |
64 |
|
|
|
65 |
douglas |
488 |
while (paths->MoveNext()) |
66 |
douglas |
555 |
files.InsertLast(MediaFile(connector, paths->GetString(_B("path")))); |
67 |
douglas |
478 |
|
68 |
douglas |
555 |
connector->Release(connection); |
69 |
|
|
|
70 |
douglas |
478 |
return files; |
71 |
|
|
} |
72 |
|
|
|
73 |
douglas |
554 |
_L<MediaFolder> MediaFolder::GetFolders(unsigned page) const |
74 |
douglas |
478 |
{ |
75 |
douglas |
555 |
_R<dbi::Connection> connection(connector->Connect()); |
76 |
douglas |
554 |
_R<dbi::ResultSet> paths(connection->Parse(_B("SELECT path FROM folders WHERE folder = ? ORDER BY path LIMIT 50 OFFSET ?"))->Execute(path.GetPath(), page * 50)); |
77 |
douglas |
478 |
_L<MediaFolder> folders; |
78 |
|
|
|
79 |
douglas |
488 |
while (paths->MoveNext()) |
80 |
douglas |
555 |
folders.InsertLast(MediaFolder(connector, paths->GetString(_B("path")))); |
81 |
|
|
|
82 |
|
|
connector->Release(connection); |
83 |
douglas |
478 |
|
84 |
|
|
return folders; |
85 |
|
|
} |