protected function main() { $this->init(); $remotePath = $this->input->getArgument('remote_path') ?: ''; if ($this->input->getOption('id')) { if (!($node = Node::loadById($remotePath))) { throw new \Exception("No node exists with ID '{$remotePath}'."); } } else { if (!($node = Node::loadByPath($remotePath))) { throw new \Exception("No node exists at remote path '{$remotePath}'."); } } if ($node->isFolder()) { throw new \Exception("Links can only be created for files."); } $response = $node->getMetadata(true); if ($response['success']) { if (isset($response['data']['tempLink'])) { $this->output->writeln($response['data']['tempLink']); } else { $this->output->getErrorOutput()->writeln("<error>Failed retrieving temporary link. Make sure you have permission.</error>"); } } else { $this->output->getErrorOutput()->writeln("<error>Failed retrieving metadata for node '{$remotePath}'</error>"); } }
protected function main() { $this->init(); $remotePath = $this->input->getArgument('remote_path'); $savePath = $this->input->getArgument('local_path') ?: getcwd(); if ($this->input->getOption('id')) { if (!($node = Node::loadById($remotePath))) { throw new \Exception("No node exists with ID '{$remotePath}'."); } } else { if (!($node = Node::loadByPath($remotePath))) { throw new \Exception("No node exists at remote path '{$remotePath}'."); } } $node->download($savePath, function ($result, $dest) { if ($result['success']) { $this->output->writeln("<info>Successfully downloaded file to '{$dest}'</info>"); } else { $this->output->getErrorOutput()->writeln("<error>Failed to download node to '{$dest}'</error>"); if ($this->output->isVerbose()) { $this->output->getErrorOutput()->writeln(json_encode($result['data'])); } } }); }
protected function main() { $this->init(); $remotePath = $this->input->getArgument('remote_path'); if ($this->input->getOption('id')) { if (!($node = Node::loadById($remotePath))) { throw new \Exception("No node exists with ID '{$remotePath}'."); } } else { if (!($node = Node::loadByPath($remotePath))) { throw new \Exception("No node exists at remote path '{$remotePath}'."); } } $result = $node->trash(); if ($result['success']) { $this->output->writeln("<info>Successfully trashed node at '{$remotePath}'</info>"); if ($this->output->isVerbose()) { $this->output->writeln(json_encode($result['data'])); } } else { $this->output->getErrorOutput()->writeln("<error>Failed to trash node at '{$remotePath}'</error>"); if ($this->output->isVerbose()) { $this->output->getErrorOutput()->writeln(json_encode($result['data'])); } } }
protected function main() { $this->init(); $remotePath = $this->input->getArgument('remote_path'); if ($node = Node::loadByPath($remotePath)) { throw new \Exception("Node already exists at remote path '{$remotePath}'. Make sure it's not in the trash."); } $result = $this->clouddrive->createDirectoryPath($remotePath); if (!$result['success']) { $this->output->writeln("Failed to create remote path '{$remotePath}': " . json_encode($result['data'])); } else { $this->output->writeln("Successfully created remote path '{$remotePath}': " . json_encode($result['data'])); } }
protected function main() { $this->init(); $path = $this->input->getArgument('path') ?: ''; if ($this->input->getOption('id')) { if (!($node = Node::loadById($path))) { throw new \Exception("No node exists with ID '{$path}'."); } } else { if (!($node = Node::loadByPath($path))) { throw new \Exception("No node exists at remote path '{$path}'."); } } $this->output->writeln($this->convertFilesize($this->calculateTotalSize($node))); }
protected function main() { $this->initOnlineCommand(); $remotePath = $this->input->getArgument('remote_path'); if ($this->input->getOption('id')) { if (!($node = Node::loadById($remotePath))) { throw new \Exception("No node exists with ID '{$remotePath}'."); } } else { if (!($node = Node::loadByPath($remotePath))) { throw new \Exception("No node exists at remote path '{$remotePath}'."); } } if ($node->isFolder()) { throw new \Exception("Folder downloads are not currently supported."); } $node->download($this->output->getStream()); }
protected function main() { $this->init(); $path = $this->input->getArgument('path') ?: ''; if ($this->input->getOption('id')) { if (!($node = Node::loadById($path))) { throw new \Exception("No node exists with ID '{$path}'."); } } else { if (!($node = Node::loadByPath($path))) { throw new \Exception("No node exists at remote path '{$path}'."); } } if ($this->config['json.pretty']) { $this->output->writeln(json_encode($node, JSON_PRETTY_PRINT)); } else { $this->output->writeln(json_encode($node)); } }
protected function main() { $this->init(); $remotePath = $this->input->getArgument('remote_path'); if ($this->input->getOption('id')) { if (!($node = Node::loadById($remotePath))) { throw new \Exception("No node exists with ID '{$remotePath}'."); } } else { if (!($node = Node::loadByPath($remotePath))) { throw new \Exception("No node exists at remote path '{$remotePath}'."); } } $result = $node->restore(); if ($result['success']) { $this->output->writeln("Successfully restored node at '{$remotePath}': " . json_encode($result['data'])); } else { $this->output->writeln("Failed to restore node at '{$remotePath}': " . json_encode($result['data'])); } }
protected function main() { $this->init(); $path = $this->input->getArgument('path') ?: ''; $includeAssets = $this->input->getOption('assets') ? true : false; if ($this->input->getOption('id')) { if (!($node = Node::loadById($path))) { throw new \Exception("No node exists with ID '{$path}'."); } } else { if (!($node = Node::loadByPath($path))) { throw new \Exception("No node exists at remote path '{$path}'."); } } if ($this->input->getOption('markdown')) { $this->buildMarkdownTree($node, $includeAssets); } else { $this->buildAsciiTree($node, $includeAssets); } }
protected function main() { $this->init(); $nodePath = $this->input->getArgument('node'); $newPath = $this->input->getArgument('new_path'); if (!($node = Node::loadByPath($nodePath))) { throw new \Exception("No node exists at remote path '{$nodePath}'."); } if (!($newParent = Node::loadByPath($newPath))) { throw new \Exception("No node exists at remote path '{$newPath}'."); } $result = $node->move($newParent); if ($result['success']) { $this->output->writeln("<info>Successfully moved node '{$node['name']}' to '{$newPath}'</info>"); if ($this->output->isVerbose()) { $this->output->writeln(json_encode($result['data'])); } } else { $this->output->getErrorOutput()->writeln("<error>Failed to move node '{$node['name']}' to '{$newPath}'</error>"); if ($this->output->isVerbose()) { $this->output->getErrorOutput()->writeln(json_encode($result['data'])); } } }
protected function main() { $this->init(); $this->clouddrive->getAccount()->authorize(); $remotePath = $this->input->getArgument('remote_path') ?: ''; $sort = Command::SORT_BY_NAME; if ($this->input->getOption('time')) { $sort = Command::SORT_BY_TIME; } if ($this->input->getOption('id')) { if (!($node = Node::loadById($remotePath))) { throw new \Exception("No node exists with ID '{$remotePath}'."); } } else { if (!($node = Node::loadByPath($remotePath))) { throw new \Exception("No node exists at remote path '{$remotePath}'."); } } if ($node->isFolder() || $this->input->getOption('assets')) { $this->listNodes($node->getChildren(), $sort); } else { $this->listNodes([$node], $sort); } }
/** * Determine if a node matching the given path exists remotely. If a local * path is given, the MD5 will be compared as well. * * @param string $remotePath The remote path to check * @param null|string $localPath Local path of file to compare MD5 * * @return array * @throws \Exception' */ public function nodeExists($remotePath, $localPath = null) { if (is_null($file = Node::loadByPath($remotePath))) { if (!is_null($localPath)) { if (!empty($nodes = Node::loadByMd5(md5_file($localPath)))) { $ids = []; foreach ($nodes as $node) { $ids[] = $node['id']; } return ['success' => true, 'data' => ['message' => "File(s) with same MD5: " . implode(', ', $ids), 'path_match' => false, 'md5_match' => true, 'nodes' => $nodes]]; } } return ['success' => false, 'data' => ['message' => "File {$remotePath} does not exist.", 'path_match' => false, 'md5_match' => false]]; } $retval = ['success' => true, 'data' => ['message' => "File {$remotePath} exists.", 'path_match' => true, 'md5_match' => false, 'node' => $file]]; if (!is_null($localPath)) { if (!is_null($file['contentProperties']['md5'])) { if (md5_file($localPath) !== $file['contentProperties']['md5']) { $retval['data']['message'] = "File {$remotePath} exists but does not match local checksum."; } else { $retval['data']['message'] = "File {$remotePath} exists and is identical to local copy."; $retval['data']['md5_match'] = true; } } else { $retval['data']['message'] = "File {$remotePath} exists but no checksum is available."; } } return $retval; }
/** * Upload a single file to Amazon Cloud Drive. * * @param resource $resource The local path to the file to upload * @param string $remotePath The remote folder to upload the file to, including the file name * @param bool|false $overwrite Whether to overwrite the file if it already * exists remotely * @param bool $suppressDedup Disables checking for duplicates when uploading * * @return array */ public function uploadStream($resource, $remotePath, $overwrite = false, $suppressDedup = false) { $retval = ['success' => false, 'data' => [], 'response_code' => null]; $info = pathinfo($remotePath); if ($info['dirname'] == '.') { $info['dirname'] = '/'; } $remotePath = $this->getPathString($this->getPathArray($info['dirname'])); if (!($remoteFolder = Node::loadByPath($remotePath))) { $response = $this->createDirectoryPath($remotePath); if ($response['success'] === false) { return $response; } $remoteFolder = $response['data']; } $response = $this->nodeExists("{$remotePath}/{$info['basename']}", $resource); if ($response['success'] === true) { $pathMatch = $response['data']['path_match']; $md5Match = $response['data']['md5_match']; if ($pathMatch === true && $md5Match === true) { // Skip if path and MD5 match $retval['data'] = $response['data']; return $retval; } else { if ($pathMatch === true && $md5Match === false) { // If path is the same and checksum differs, only overwrite if ($overwrite === true) { return $response['data']['node']->overwrite($resource); } $retval['data'] = $response['data']; return $retval; } else { if ($pathMatch === false && $md5Match === true) { // If path differs and checksum is the same, check for dedup if ($suppressDedup === false) { $retval['data'] = $response['data']; return $retval; } } } } } $suppressDedup = $suppressDedup ? '?suppress=deduplication' : ''; $response = $this->httpClient->post("{$this->account->getContentUrl()}nodes{$suppressDedup}", ['headers' => ['Authorization' => "Bearer {$this->account->getToken()['access_token']}"], 'multipart' => [['name' => 'metadata', 'contents' => json_encode(['kind' => 'FILE', 'name' => $info['basename'], 'parents' => [$remoteFolder['id']]])], ['name' => 'file', 'contents' => $resource, 'filename' => $info['basename']]], 'exceptions' => false]); $retval['data'] = json_decode((string) $response->getBody(), true); $retval['response_code'] = $response->getStatusCode(); if (($retval['response_code'] = $response->getStatusCode()) === 201) { $retval['success'] = true; (new Node($retval['data']))->save(); } return $retval; }
/** * Get the timestamp of a file. * * @param string $path * * @return array|false */ public function getTimestamp($path) { if (!($node = Node::loadByPath($path))) { return false; } return ['timestamp' => (new \DateTime($node['modifiedDate']))->getTimestamp()]; }