1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * GitHub API Repositories Downloads class for the Joomla Platform.
14 *
15 * The downloads API is for package downloads only.
16 * If you want to get source tarballs you should use
17 * https://developer.github.com/v3/repos/contents/#get-archive-link instead.
18 *
19 * @documentation https://developer.github.com/v3/repos/downloads
20 *
21 * @since 11.3
22 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
23 */
24 class JGithubPackageRepositoriesDownloads extends JGithubPackage
25 {
26 /**
27 * List downloads for a repository.
28 *
29 * @param string $owner The name of the owner of the GitHub repository.
30 * @param string $repo The name of the GitHub repository.
31 *
32 * @since 3.3 (CMS)
33 *
34 * @return object
35 */
36 public function getList($owner, $repo)
37 {
38 // Build the request path.
39 $path = '/repos/' . $owner . '/' . $repo . '/downloads';
40
41 // Send the request.
42 return $this->processResponse(
43 $this->client->get($this->fetchUrl($path))
44 );
45 }
46
47 /**
48 * Get a single download.
49 *
50 * @param string $owner The name of the owner of the GitHub repository.
51 * @param string $repo The name of the GitHub repository.
52 * @param integer $id The id of the download.
53 *
54 * @since 3.3 (CMS)
55 *
56 * @return object
57 */
58 public function get($owner, $repo, $id)
59 {
60 // Build the request path.
61 $path = '/repos/' . $owner . '/' . $repo . '/downloads/' . $id;
62
63 // Send the request.
64 return $this->processResponse(
65 $this->client->get($this->fetchUrl($path))
66 );
67 }
68
69 /**
70 * Create a new download (Part 1: Create the resource).
71 *
72 * Creating a new download is a two step process. You must first create a new download resource.
73 *
74 * @param string $owner The name of the owner of the GitHub repository.
75 * @param string $repo The name of the GitHub repository.
76 * @param string $name The name.
77 * @param string $size Size of file in bytes.
78 * @param string $description The description.
79 * @param string $content_type The content type.
80 *
81 * @since 3.3 (CMS)
82 *
83 * @return object
84 */
85 public function create($owner, $repo, $name, $size, $description = '', $content_type = '')
86 {
87 // Build the request path.
88 $path = '/repos/' . $owner . '/' . $repo . '/downloads';
89
90 $data = array(
91 'name' => $name,
92 'size' => $size,
93 );
94
95 if ($description)
96 {
97 $data['description'] = $description;
98 }
99
100 if ($content_type)
101 {
102 $data['content_type'] = $content_type;
103 }
104
105 // Send the request.
106 return $this->processResponse(
107 $this->client->post($this->fetchUrl($path), $data),
108 201
109 );
110 }
111
112 /**
113 * Create a new download (Part 2: Upload file to s3).
114 *
115 * Now that you have created the download resource, you can use the information
116 * in the response to upload your file to s3. This can be done with a POST to
117 * the s3_url you got in the create response. Here is a brief example using curl:
118 *
119 * curl \
120 * -F "key=downloads/octocat/Hello-World/new_file.jpg" \
121 * -F "acl=public-read" \
122 * -F "success_action_status=201" \
123 * -F "Filename=new_file.jpg" \
124 * -F "AWSAccessKeyId=1ABCDEF..." \
125 * -F "Policy=ewogIC..." \
126 * -F "Signature=mwnF..." \
127 * -F "Content-Type=image/jpeg" \
128 * -F "file=@new_file.jpg" \
129 * https://github.s3.amazonaws.com/
130 *
131 * NOTES
132 * The order in which you pass these fields matters! Follow the order shown above exactly.
133 * All parameters shown are required and if you excluded or modify them your upload will
134 * fail because the values are hashed and signed by the policy.
135 *
136 * More information about using the REST API to interact with s3 can be found here:
137 * http://docs.amazonwebservices.com/AmazonS3/latest/API/
138 *
139 * @param string $key Value of path field in the response.
140 * @param string $acl Value of acl field in the response.
141 * @param string $success_action_status 201, or whatever you want to get back.
142 * @param string $filename Value of name field in the response.
143 * @param string $awsAccessKeyId Value of accesskeyid field in the response.
144 * @param string $policy Value of policy field in the response.
145 * @param string $signature Value of signature field in the response.
146 * @param string $content_type Value of mime_type field in the response.
147 * @param string $file Local file. Example assumes the file existing in the directory
148 * where you are running the curl command. Yes, the @ matters.
149 *
150 * @since 3.3 (CMS)
151 *
152 * @return boolean
153 */
154 public function upload($key, $acl, $success_action_status, $filename, $awsAccessKeyId, $policy, $signature, $content_type, $file)
155 {
156 // Build the request path.
157 $url = 'https://github.s3.amazonaws.com/';
158
159 $data = array(
160 'key' => $key,
161 'acl' => $acl,
162 'success_action_status' => (int) $success_action_status,
163 'Filename' => $filename,
164 'AWSAccessKeyId' => $awsAccessKeyId,
165 'Policy' => $policy,
166 'Signature' => $signature,
167 'Content-Type' => $content_type,
168 'file' => $file,
169 );
170
171 // Send the request.
172 $response = $this->client->post($url, $data);
173
174 // @todo Process the response..
175
176 return (201 == $response->code) ? true : false;
177 }
178
179 /**
180 * Delete a download.
181 *
182 * @param string $owner The name of the owner of the GitHub repository.
183 * @param string $repo The name of the GitHub repository.
184 * @param integer $id The id of the download.
185 *
186 * @since 3.3 (CMS)
187 *
188 * @return object
189 */
190 public function delete($owner, $repo, $id)
191 {
192 // Build the request path.
193 $path = '/repos/' . $owner . '/' . $repo . '/downloads/' . (int) $id;
194
195 // Send the request.
196 return $this->processResponse(
197 $this->client->delete($this->fetchUrl($path)),
198 204
199 );
200 }
201 }
202