-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
156 lines (124 loc) · 4.87 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
require __DIR__ . '/vendor/autoload.php';
use JeanSouzaK\Duf\Download\DownloadOptions;
use JeanSouzaK\Duf\Duff;
use JeanSouzaK\Duf\Prepare\WebResource;
use JeanSouzaK\Duf\Filter\WebFileSizeFilter;
use JeanSouzaK\Duf\Filter\WebFileExtensionFilter;
use JeanSouzaK\Duf\Filter\LocalFileSizeFilter;
use JeanSouzaK\Duf\Filter\LocalFileExtensionFilter;
use JeanSouzaK\Duf\Filter\PathFilterable;
use JeanSouzaK\Duf\Filter\Filterable;
use JeanSouzaK\Duf\Filter\HeaderFilterable;
use JeanSouzaK\Duf\Prepare\LocalResource;
$duf = Duff::getInstance(Duff::GOOGLE_CLOUD_STORAGE, [
'project_id' => 'mybucket-280818',
'key_path' => './mybucket-280818-14d6f8d296cd.json'
]);
// - Chaining example -
$uploadResults = $duf->prepare([
new WebResource('teste.png', 'https://dummyimage.com/600x400/000/fff')
])->download()->addBucket('dufbuckettest')->upload();
// - Step-by-step example -
//Prepare object name and url
$duf->prepare([
new WebResource('object_one.png', 'https://dummyimage.com/600x400/000/fff'),
new WebResource('object_two.png', 'https://dummyimage.com/300x200/000/fff'),
new WebResource('object_three.png', 'https://dummyimage.com/100x100/000/fff')
]);
//Make download prepared files
$duf->download();
//Add google cloud storage bucket name
$duf->addBucket('dufbuckettest');
//Upload downloaded files to bucket and get results
$uploadResults = $duf->upload();
// Iterate results and get result path
echo '<pre>';
foreach ($uploadResults as $uploadResult) {
print_r($uploadResult);
echo (string)$uploadResult.'<br/>';
}
//Uploading from local file target
$duf->prepare([
new LocalResource('imagem', '/home/test/images/test.jpg')
]);
//You can mix local and remote target file
$duf->prepare([
new LocalResource('imagem', '/home/test/images/test.jpg'),
new WebResource('teste.png', 'https://dummyimage.com/600x400/000/fff')
]);
//Make download prepared files
$duf->download();
//Add google cloud storage bucket name
$duf->addBucket('dufbuckettest');
//Upload downloaded files to bucket and get results
$fileResults = $duf->upload();
//Call Factory to get google cloud storage duffer instance
$duf = Duff::getInstance(Duff::GOOGLE_CLOUD_STORAGE, [
'project_id' => 'mybucket-280818',
'key_path' => './mybucket-280818-14d6f8d296cd.json'
]);
//[WEB Filter] Configure a maximum file size to bind in a resource
$webPngSizeFilter = new WebFileSizeFilter(2, WebFileSizeFilter::MB);
//[WEB Filter] Configure alloweed extensions to bind in a resource
$webAllowedExtensionFilter = new WebFileExtensionFilter([
WebFileExtensionFilter::JPEG,
WebFileExtensionFilter::PNG
]);
$localJpgSizeFilter = new LocalFileSizeFilter(1, 'M');
$localAllowedExtensionFilter = new LocalFileExtensionFilter([
'json',
'doc',
'docx'
]);
$downloadOptions = new DownloadOptions();
$downloadOptions->setInduceType(true);
//Prepare object name, url and filters (second resource is a GIF contenttype not allowed format)
$duf->prepare([
new WebResource('object_one.png', 'https://dummyimage.com/600x400/000/fff', [$webPngSizeFilter], $downloadOptions),
new WebResource('object_two.png', 'https://dummyimage.com/600x400/000/fff.gif', [$webAllowedExtensionFilter]),
new WebResource('object_three.png', 'https://dummyimage.com/100x100/000/fff', [$webPngSizeFilter, $webAllowedExtensionFilter]),
new LocalResource('imagem', '/home/test/images/test.jpg', [$localJpgSizeFilter]),
new LocalResource('imagem', '/home/test/project/composer.json', [$localAllowedExtensionFilter]),
]);
//Create DownloadOptions(optional)
$downloadOptions = new DownloadOptions();
//Telling Duf to induce the file extension(default false)
$downloadOptions->setInduceType(true);
//Using file URL to induce file extension(defaul INDUCE_FROM_BOTH)
$downloadOptions->setInduceMethod(DownloadOptions::INDUCE_FROM_URL);
//Make download prepared files
$duf->download($downloadOptions);
//Add google cloud storage bucket name
$duf->addBucket('dufbuckettest');
//Upload downloaded files to bucket and get results
$fileResults = $duf->upload();
// Iterate results and get result path
foreach ($fileResults as $fileResult) {
echo (string)$fileResult.'<br/>';
}
//You can dump result objects to see more details
//Working with result object:
/** @var File $fileResult */
foreach ($fileResults as $fileResult) {
if($fileResult->hasError()){
echo "File {$fileResult->getName()} contains error: {$fileResult->getErrorMessage()}<br/>";
continue;
}
echo "File {$fileResult->getName()} uploaded sucessfull<br/>";
}
//Write your own filters
class MyLocalFilter implements Filterable, PathFilterable
{
public function applyPathFilters(array $filePathInfo)
{
//throw exception with message
}
}
class MyWebFilter implements Filterable, HeaderFilterable
{
public function applyHeaderFilter(array $fileHeadersInfo)
{
//throw exception with message
}
}