1: <?php
2:
3: namespace webfilesframework\core\datasystem\file\system\dropbox;
4:
5: use webfilesframework\core\datasystem\file\system\MDirectory;
6:
7: 8: 9: 10: 11: 12:
13: class MDropboxDirectory extends MDirectory
14: {
15:
16: private $dropboxAccount;
17: private $dropboxFolderPath;
18: private $directoryMetadata;
19:
20: 21: 22: 23: 24:
25: public function __construct($path, MDropboxAccount $dropboxAccount)
26: {
27: parent::__construct($path);
28: $this->dropboxFolderPath = $path;
29: $this->dropboxAccount = $dropboxAccount;
30: $this->directoryMetadata = $this->dropboxAccount->getDropboxApi()->metaData($path);
31:
32: }
33:
34: public function getFiles($initMetadata = true)
35: {
36:
37: if ($initMetadata) {
38:
39: } else {
40:
41: }
42:
43: $files = array();
44: $metdadataBody = $this->directoryMetadata['body'];
45:
46: foreach ($metdadataBody->contents as $value) {
47: if (!$value->is_dir) {
48: $dropboxFile = new MDropboxFile($this->dropboxAccount, $value->path, $initMetadata);
49: $date = $value->modified;
50: $dropboxFile->setDate($date);
51: array_push($files, $dropboxFile);
52: }
53: }
54:
55: return $files;
56:
57: }
58:
59: public function getLatestFiles($count)
60: {
61:
62: $filesArray = $this->getFiles(false);
63:
64:
65: $latestFilesArray = array_slice($filesArray, $count * -1);
66:
67: foreach ($latestFilesArray as $latestFile) {
68:
69: }
70: return $latestFilesArray;
71: }
72:
73:
74: public function getFileNames()
75: {
76: $fileNames = array();
77: $metdadataBody = $this->directoryMetadata['body'];
78:
79: foreach ($metdadataBody->contents as $value) {
80: if (!$value->is_dir) {
81: $lastSlash = strrpos($value->path, '/');
82: $path = substr($value->path, $lastSlash + 1);
83: array_push($fileNames, $path);
84: }
85: }
86:
87: return $fileNames;
88: }
89:
90: public function getSubdirectories()
91: {
92: $subdirectories = array();
93: $metdadataBody = $this->directoryMetadata['body'];
94:
95: foreach ($metdadataBody['contents'] as $value) {
96: if (!$value->is_dir) {
97: $dropboxDirectory = new MDropboxDirectory($this->dropboxAccount, $value->path);
98: array_push($subdirectories, $dropboxDirectory);
99: }
100: }
101:
102: return $subdirectories;
103: }
104:
105:
106: 107: 108: 109: 110: 111:
112: public function syncToLocalDirectory(MDirectory $directory, $recursivly, $folder)
113: {
114:
115:
116:
117: $folder = $folder['body'];
118:
119:
120:
121: $dropboxFolderPath = new MDirectory($directory->getFolderName() . "/" . $folder->path);
122: if (!$dropboxFolderPath->exists()) {
123: $dropboxFolderPath->create();
124: }
125:
126: $files = $directory->getFiles();
127: $contents = $folder->contents;
128:
129: if (sizeof($files) != sizeof($contents) + 2) {
130:
131:
132: $reverse = array_reverse($contents);
133:
134: foreach ($reverse as $value) {
135:
136:
137: if (!$value->is_dir) {
138:
139: if ($value->mime_type == 'image/jpeg') {
140:
141: $this->downloadImage($directory, utf8_encode($value->path));
142: } else {
143: $this->downloadFile($directory, utf8_encode($value->path));
144: }
145: } else {
146:
147: if ($recursivly) {
148: if (!file_exists("." . $value->path)) {
149:
150: $dropboxDirectory = new MDropboxDirectory($value->path, $this->dropboxAccount);
151:
152: $this->syncToLocalDirectory(
153:
154: $directory,
155: true,
156: $dropboxDirectory->getFolderMetadata()
157: );
158: }
159: }
160: }
161: }
162:
163: }
164: }
165:
166: 167: 168: 169: 170:
171: private function downloadFile(MDirectory $rootFolder, $filePath)
172: {
173:
174: $file = $this->dropboxAccount->getDropboxApi()->getFile($filePath);
175: $filePath = $rootFolder->getFolderName() . $filePath;
176: $this->saveToFilesystem($filePath, $file);
177: }
178:
179: 180: 181: 182: 183:
184: private function downloadImage(MDirectory $rootFolder, $filePath)
185: {
186:
187: $file = $this->dropboxAccount->getDropboxApi()->thumbnails($filePath, 'JPEG', 'l');
188: $filePath = $rootFolder->getFolderName() . $filePath;
189: $this->saveToFilesystem($filePath, $file);
190: }
191:
192: 193: 194: 195: 196:
197: private function saveToFilesystem($filePath, $file)
198: {
199: if (!file_exists("." . $filePath)) {
200: $fp = fopen("." . $filePath, "w");
201: fputs($fp, $file['data']);
202: fclose($fp);
203: }
204: }
205:
206: 207: 208: 209:
210: public function getFolderMetadata()
211: {
212: return $this->directoryMetadata;
213: }
214:
215: }
216: