|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Modules/Filesystem |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv2\Modules\Filesystem; |
|
45
|
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\HostLib; |
|
47
|
|
|
use DataSift\Storyplayer\OsLib; |
|
48
|
|
|
|
|
49
|
|
|
use DataSift\Stone\DataLib\DataPrinter; |
|
50
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
|
51
|
|
|
|
|
52
|
|
|
use GanbaroDigital\TextTools\Filters\FilterColumns; |
|
53
|
|
|
use GanbaroDigital\TextTools\Filters\FilterForMatchingRegex; |
|
54
|
|
|
use GanbaroDigital\TextTools\Filters\FilterForMatchingString; |
|
55
|
|
|
|
|
56
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
|
57
|
|
|
use Storyplayer\SPv2\Modules\Host\HostAwareModule; |
|
58
|
|
|
use Storyplayer\SPv2\Modules\Log; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* get information about a given host |
|
62
|
|
|
* |
|
63
|
|
|
* @category Libraries |
|
64
|
|
|
* @package Storyplayer/Modules/Filesystem |
|
65
|
|
|
* @author Stuart Herbert <[email protected]> |
|
66
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
67
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
68
|
|
|
* @link http://datasift.github.io/storyplayer |
|
69
|
|
|
*/ |
|
70
|
|
|
class FromFilesystem extends HostAwareModule |
|
71
|
|
|
{ |
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $sourceFilename |
|
74
|
|
|
* @param string $destFilename |
|
75
|
|
|
* @return \DataSift\Storyplayer\CommandLib\CommandResult |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function downloadFile($sourceFilename, $destFilename) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
// what are we doing? |
|
80
|
|
|
$log = Log::usingLog()->startAction("download file '{$this->args[0]}':'{$sourceFilename}' to '{$destFilename}'"); |
|
81
|
|
|
|
|
82
|
|
|
// make sure we have valid host details |
|
83
|
|
|
$hostDetails = $this->getHostDetails(); |
|
84
|
|
|
|
|
85
|
|
|
// get an object to talk to this host |
|
86
|
|
|
$host = OsLib::getHostAdapter($this->st, $hostDetails->osName); |
|
87
|
|
|
|
|
88
|
|
|
// upload the file |
|
89
|
|
|
$result = $host->downloadFile($hostDetails, $sourceFilename, $destFilename); |
|
90
|
|
|
|
|
91
|
|
|
// did the command used to upload succeed? |
|
92
|
|
|
if ($result->didCommandFail()) { |
|
93
|
|
|
$msg = "download failed with return code '{$result->returnCode}' and output '{$result->output}'"; |
|
94
|
|
|
$log->endAction($msg); |
|
95
|
|
|
throw Exceptions::newActionFailedException(__METHOD__, $msg); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// all done |
|
99
|
|
|
$log->endAction(); |
|
100
|
|
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $filename |
|
105
|
|
|
* @return object |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getFileDetails($filename) |
|
108
|
|
|
{ |
|
109
|
|
|
// what are we doing? |
|
110
|
|
|
$log = Log::usingLog()->startAction("get details for '{$filename}' on host '{$this->args[0]}'"); |
|
111
|
|
|
|
|
112
|
|
|
// make sure we have valid host details |
|
113
|
|
|
$hostDetails = $this->getHostDetails(); |
|
114
|
|
|
|
|
115
|
|
|
// get an object to talk to this host |
|
116
|
|
|
$host = OsLib::getHostAdapter($this->st, $hostDetails->osName); |
|
117
|
|
|
|
|
118
|
|
|
// get the details |
|
119
|
|
|
$details = $host->getFileDetails($hostDetails, $filename); |
|
120
|
|
|
|
|
121
|
|
|
// all done |
|
122
|
|
|
$log->endAction(); |
|
123
|
|
|
return $details; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.