Skip to content

Commit 8a1f5a9

Browse files
authored
Initial version squashed commits (#8)
1 parent 8a75126 commit 8a1f5a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3673
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
tmp*
3+
composer.lock
4+
vendor

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# docker container with PHP and the Storj test network
2+
FROM php:8.0-cli
3+
4+
RUN echo "deb http://deb.debian.org/debian buster-backports main" >> /etc/apt/sources.list \
5+
&& apt-get update \
6+
&& apt-get install -y libffi-dev postgresql git redis-server wget \
7+
&& docker-php-ext-install ffi
8+
9+
# Go 1.15 is not in debian repository buster-backports yet
10+
RUN wget https://dl.google.com/go/go1.15.8.linux-amd64.tar.gz \
11+
&& tar -xvf go1.15.8.linux-amd64.tar.gz \
12+
&& mv go /usr/local
13+
14+
ENV GOROOT=/usr/local/go
15+
ENV PATH="/usr/local/go/bin:${PATH}"
16+
17+
RUN rm /etc/postgresql/11/main/pg_hba.conf; \
18+
echo 'local all all trust' >> /etc/postgresql/11/main/pg_hba.conf; \
19+
echo 'host all all 127.0.0.1/8 trust' >> /etc/postgresql/11/main/pg_hba.conf; \
20+
echo 'host all all ::1/128 trust' >> /etc/postgresql/11/main/pg_hba.conf; \
21+
echo 'host all all ::0/0 trust' >> /etc/postgresql/11/main/pg_hba.conf;
22+
23+
RUN git clone https://github.com/storj/storj.git /storj \
24+
&& cd /storj \
25+
&& make install-sim

Jenkinsfile

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
pipeline {
2+
agent none
3+
4+
options {
5+
timeout(time: 20, unit: 'MINUTES')
6+
}
7+
stages {
8+
stage('Go build') {
9+
agent {
10+
docker {
11+
image docker.build("storj-ci", "--pull https://github.com/storj/ci.git#main").id
12+
args '--user root:root --cap-add SYS_PTRACE -v "/tmp/gomod":/go/pkg/mod '
13+
}
14+
}
15+
steps {
16+
script {
17+
sh './build.sh'
18+
stash(name: "build", includes: "build/")
19+
}
20+
}
21+
}
22+
stage('Release') {
23+
agent {
24+
docker {
25+
image 'kramos/alpine-zip:latest'
26+
args '--entrypoint ""'
27+
}
28+
}
29+
steps {
30+
unstash "build"
31+
sh "zip -r release.zip *"
32+
archiveArtifacts "release.zip"
33+
}
34+
}
35+
stage('Composer') {
36+
agent {
37+
docker {
38+
image 'composer:2.0.11'
39+
args '--mount type=volume,source=composer-cache,destination=/root/.composer/cache '
40+
}
41+
}
42+
steps {
43+
sh 'composer install --ignore-platform-reqs'
44+
stash(name: "vendor", includes: "vendor/")
45+
}
46+
}
47+
stage('PHPStan') {
48+
agent {
49+
docker {
50+
image 'phpstan/phpstan:0.12.80'
51+
args '--mount type=volume,source=phpstan-cache,destination=/tmp/phpstan ' +
52+
'-u root:root ' +
53+
"--entrypoint='' "
54+
}
55+
}
56+
steps {
57+
unstash "vendor"
58+
sh 'phpstan analyse'
59+
}
60+
}
61+
stage('PHPUnit') {
62+
agent {
63+
dockerfile {
64+
args '-u root:root'
65+
}
66+
}
67+
steps {
68+
unstash "vendor"
69+
unstash "build"
70+
sh 'service postgresql start'
71+
sh '''su -s /bin/bash -c "psql -U postgres -c 'create database teststorj;'" postgres'''
72+
sh 'PATH="/root/go/bin:$PATH" && storj-sim network setup --postgres=postgres://postgres@localhost/teststorj?sslmode=disable'
73+
// see https://github.com/storj/storj/wiki/Test-network#running-tests
74+
sh 'PATH="/root/go/bin:$PATH" && storj-sim network test ./vendor/bin/phpunit test/'
75+
}
76+
}
77+
}
78+
post {
79+
always {
80+
node(null) {
81+
cleanWs()
82+
}
83+
}
84+
}
85+
}

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Storj Labs, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

MAINTAINERS.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Automated testing
2+
-----
3+
4+
Install dependencies using `composer install`, then set up an environment with your tardigrade credentials:
5+
6+
```
7+
#!/bin/bash
8+
9+
export SATTELITE_ADDRESS="europe-west-1.tardigrade.io:7777"
10+
export GATEWAY_0_API_KEY="base58stuff"
11+
12+
php vendor/bin/phpunit test/
13+
```
14+
15+
New release after uplink-c version bump
16+
--------------------------
17+
18+
- Change the uplink-c git tag in ./build.sh
19+
- Let jenkins build the artifact
20+
- Create a git tag
21+
- Update the tag and artifact URL in [README.md#installation](./README.md#installation)

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
Uplink PHP client
2+
=============
3+
4+
Client for decentralized object storage on [Tardigrade.io](https://tardigrade.io/)
5+
6+
[▶ 40 second video](https://www.youtube.com/watch?v=QOjM5ERd8yo&feature=youtu.be)
7+
8+
Requirements
9+
----------
10+
11+
- Tardigrade API key
12+
- PHP >= 7.4
13+
- Linux x64
14+
15+
Installation
16+
---------
17+
18+
1. Enable the FFI in php.ini:
19+
20+
```
21+
extension=ffi
22+
```
23+
24+
2. (optional) To use it in web request also adjust this setting from "preload" to "true":
25+
26+
```
27+
ffi.enable=true
28+
```
29+
30+
3. Install using composer. Copy and run:
31+
32+
```
33+
composer config repositories.storj/uplink '{
34+
"type": "package",
35+
"package": {
36+
"name": "storj/uplink",
37+
"version": "0.1.0",
38+
"license": "MIT/Expat",
39+
"dist": {
40+
"url": "https://build.dev.storj.io/job/uplink-php/job/PR-8/14/artifact/release.zip",
41+
"type": "zip"
42+
},
43+
"autoload": {
44+
"psr-4": {
45+
"Storj\\Uplink\\": "src/"
46+
}
47+
},
48+
"autoload-dev": {
49+
"psr-4": {
50+
"Storj\\Uplink\\Test\\": "test/"
51+
}
52+
},
53+
"require": {
54+
"php": ">=7.4",
55+
"ext-ffi": "*",
56+
"psr/http-message": "^1.0"
57+
},
58+
"require-dev": {
59+
"phpunit/phpunit": "^9.2"
60+
}
61+
}
62+
}' &&
63+
composer require storj/uplink
64+
```
65+
66+
Usage
67+
----
68+
69+
For better response times of your web app, you should serialize your credentials to an access string.
70+
71+
```php
72+
require 'vendor/autoload.php';
73+
$access = \Storj\Uplink\Uplink::create()->requestAccessWithPassphrase(
74+
'12L9ZFwhzVpuEKMUNUqkaTLGzwY9G24tbiigLiXpmZWKwmcNDDs@europe-west-1.tardigrade.io:7777',
75+
'mybase58apikey',
76+
'mypassphrase'
77+
);
78+
$serialized = $access->serialize();
79+
echo $serialized;
80+
```
81+
82+
If you have already used uplink-cli you can read this from `~/.local/share/storj/uplink/config.yaml`
83+
or share it using `$ uplink share`
84+
85+
You can then use this in your web app:
86+
87+
```php
88+
$access = \Storj\Uplink\Uplink::create()->parseAccess($serialized);
89+
```
90+
91+
Examples:
92+
93+
- [raw/upload.php](examples/raw/upload.php) Upload files via a HTML form
94+
- [raw/download.php](examples/raw/download.php) Download files via the browser
95+
- [psr/FormUploadHandler.php](examples/psr/FormUploadHandler.php) Upload files via a HTML form in a PSR-7 framework
96+
- [psr/DownloadHandler.php](examples/psr/FormUploadHandler.php) Stream a PSR-7 HTTP response
97+

build.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -e
3+
rm -rf tmp-c
4+
mkdir tmp-c
5+
git clone --branch v1.2.2 https://github.com/storj/uplink-c.git tmp-c
6+
cd tmp-c
7+
## prefer Go release >=1.15 (aug-2020) because it preserves parameter names in the header file
8+
make build
9+
cd ..
10+
mkdir -p build
11+
cat tmp-c/.build/uplink/uplink_definitions.h tmp-c/.build/uplink/uplink.h > build/uplink-php.h
12+
cat tmp-c/.build/libuplink.so > build/libuplink.so
13+
## remove stuff PHP can't handle
14+
sed -i 's/typedef __SIZE_TYPE__ GoUintptr;//g' build/uplink-php.h
15+
sed -i 's/typedef float _Complex GoComplex64;//g' build/uplink-php.h
16+
sed -i 's/typedef double _Complex GoComplex128;//g' build/uplink-php.h
17+
sed -i 's/#ifdef __cplusplus//g' build/uplink-php.h
18+
sed -i 's/extern "C" {//g' build/uplink-php.h
19+
sed -i 's/#endif//g' build/uplink-php.h
20+
sed -zi 's/}\n//g' build/uplink-php.h
21+
22+
23+

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "storj/uplink",
3+
"description": "Decentralized object storage on Tardigrade",
4+
"homepage": "https://github.com/storj-thirdparty/uplink-php",
5+
"type": "library",
6+
"license": "MIT/Expat",
7+
"authors": [
8+
{
9+
"name": "Erik van Velzen",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"Storj\\Uplink\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Storj\\Uplink\\Test\\": "test/"
21+
}
22+
},
23+
"require": {
24+
"php": ">=7.4",
25+
"ext-ffi": "*",
26+
"psr/http-message": "^1.0"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "^9.2"
30+
}
31+
}

examples/psr/DownloadHandler.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Handler;
4+
5+
use Psr\Http\Message\ResponseFactoryInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use Psr\Http\Server\RequestHandlerInterface;
9+
use Storj\Uplink\Access;
10+
use Storj\Uplink\Exception\Object\ObjectNotFound;
11+
12+
/**
13+
* Minimal example to download a file using PHP-FIG standards.
14+
* Verified working in Mezzio.
15+
*/
16+
class DownloadHandler implements RequestHandlerInterface
17+
{
18+
private Access $access;
19+
20+
private ResponseFactoryInterface $responseFactory;
21+
22+
public function __construct(Access $access, ResponseFactoryInterface $responseFactory)
23+
{
24+
$this->access = $access;
25+
$this->responseFactory = $responseFactory;
26+
}
27+
28+
public function handle(ServerRequestInterface $request): ResponseInterface
29+
{
30+
$path = $request->getUri()->getPath();
31+
$filename = basename($path); // or get a route parameter via $request->getAttribute()
32+
33+
$project = $this->access->openProject();
34+
try {
35+
$download = $project->downloadObject('psr-uploads', $filename);
36+
} catch (ObjectNotFound $e) {
37+
$response = $this->responseFactory->createResponse(404);
38+
$response->getBody()->write('<p>file not found</p>');
39+
return $response;
40+
}
41+
42+
$response = $this->responseFactory->createResponse(200);
43+
$response = $response->withBody($download->toPsrStream());
44+
45+
$contentType = $download->info()->getCustomMetadata()['Content-Type'] ?? null;
46+
if ($contentType) {
47+
$response = $response->withHeader('Content-Type', $contentType);
48+
}
49+
50+
return $response;
51+
}
52+
}

0 commit comments

Comments
 (0)