Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
AddIpfs | |
0.00% |
0 / 49 |
|
0.00% |
0 / 5 |
110 | |
0.00% |
0 / 1 |
downloadPdf | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
uploadToIPFS | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
pinToIPFS | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @ingroup Maintenance |
19 | */ |
20 | |
21 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
22 | |
23 | class AddIpfs extends Maintenance { |
24 | |
25 | function downloadPdf( $url ) { |
26 | // Use MediaWiki's HttpRequestFactory to download the PDF |
27 | $httpRequestFactory = $this->getServiceContainer()->getHttpRequestFactory(); |
28 | |
29 | $request = $httpRequestFactory->create( $url, [ 'followRedirects' => true ] ); |
30 | $request->execute(); |
31 | $response = $request->getContent(); |
32 | if ( !$response ) { |
33 | throw new Exception( 'Failed to download the PDF.' ); |
34 | } |
35 | |
36 | return $response; |
37 | } |
38 | |
39 | function uploadToIPFS( $pdfData ) { |
40 | // Define the local IPFS API endpoint |
41 | $ipfsApiUrl = 'http://127.0.0.1:5001/api/v0/add'; |
42 | |
43 | // Use cURL to upload the PDF to IPFS |
44 | $ch = curl_init(); |
45 | curl_setopt( $ch, CURLOPT_URL, $ipfsApiUrl ); |
46 | curl_setopt( $ch, CURLOPT_POST, true ); |
47 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
48 | |
49 | // Create a temporary file for the PDF |
50 | $tempFile = tmpfile(); |
51 | fwrite( $tempFile, $pdfData ); |
52 | fseek( $tempFile, 0 ); |
53 | |
54 | // Get the file's metadata |
55 | $metaData = stream_get_meta_data( $tempFile ); |
56 | $tempFilePath = $metaData['uri']; |
57 | |
58 | // Prepare the file to be uploaded |
59 | $file = new CURLFile( $tempFilePath, 'application/pdf', 'document.pdf' ); |
60 | $postFields = [ 'file' => $file ]; |
61 | |
62 | // Attach the file to the request |
63 | curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields ); |
64 | |
65 | // Execute the request |
66 | $response = curl_exec( $ch ); |
67 | $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); |
68 | |
69 | if ( $httpCode !== 200 || $response === false ) { |
70 | die( 'Failed to upload to IPFS. HTTP Code: ' . $httpCode . '. Error: ' . curl_error( $ch ) ); |
71 | } |
72 | |
73 | curl_close( $ch ); |
74 | |
75 | // Parse the IPFS response to get the CID |
76 | $json = json_decode( $response, true ); |
77 | |
78 | // Close and delete the temporary file |
79 | fclose( $tempFile ); |
80 | |
81 | // Return the CID |
82 | return $json['Hash']; |
83 | } |
84 | |
85 | function pinToIPFS( $cid ) { |
86 | // Define the IPFS API endpoint for pinning |
87 | $ipfsPinApiUrl = 'http://127.0.0.1:5001/api/v0/pin/add?arg=' . $cid; |
88 | |
89 | // Use MediaWiki's HttpRequestFactory to pin the CID |
90 | $httpRequestFactory = $this->getServiceContainer()->getHttpRequestFactory(); |
91 | |
92 | // Create the request |
93 | $request = $httpRequestFactory->create( $ipfsPinApiUrl, [ 'method' => 'POST' ] ); |
94 | |
95 | // Execute the request |
96 | $request->execute(); |
97 | |
98 | // Get the content of the response |
99 | $response = $request->getContent(); |
100 | |
101 | if ( $response === false ) { |
102 | throw new Exception( 'Failed to pin CID.' ); |
103 | } |
104 | |
105 | // Parse the IPFS response to confirm pinning |
106 | $json = json_decode( $response, true ); |
107 | if ( isset( $json['Pins'] ) ) { |
108 | return 'CID ' . $cid . ' successfully pinned on your local IPFS node.'; |
109 | } else { |
110 | throw new Exception( 'Failed to pin CID: ' . $cid ); |
111 | } |
112 | } |
113 | |
114 | public function __construct() { |
115 | parent::__construct(); |
116 | $this->addDescription( "Uploads a http URL to IPFS and returns the CID" ); |
117 | $this->addArg( 'url', 'The url to be downloaded.', true ); |
118 | } |
119 | |
120 | public function execute() { |
121 | // Step 1: Download the PDF from the provided URL |
122 | $pdfData = $this->downloadPdf( $this->getArg( 0 ) ); |
123 | |
124 | // Step 2: Upload the PDF to IPFS and get the CID |
125 | $cid = $this->uploadToIPFS( $pdfData ); |
126 | |
127 | // Step 3: Pin the CID on your local IPFS node |
128 | $pinStatus = $this->pinToIPFS( $cid ); |
129 | |
130 | // Step 4: Return the CID and pin status |
131 | var_dump( [ |
132 | 'cid' => $cid, |
133 | 'pinStatus' => $pinStatus, |
134 | ] ); |
135 | } |
136 | } |
137 | |
138 | $maintClass = AddIpfs::class; |
139 | /** @noinspection PhpIncludeInspection */ |
140 | require_once RUN_MAINTENANCE_IF_MAIN; |