1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This class serves as a skeleton for Cloud Transcode classes implementing actual activities |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2016 BFan Sports - Sport Archive Inc. |
7
|
|
|
* |
8
|
|
|
* This program is free software; you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License along |
19
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
20
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
require_once __DIR__."/../../vendor/autoload.php"; |
24
|
|
|
require_once __DIR__.'/../utils/S3Utils.php'; |
25
|
|
|
|
26
|
|
|
use SA\CpeSdk; |
27
|
|
|
|
28
|
|
|
class BasicActivity extends CpeSdk\CpeActivity |
29
|
|
|
{ |
30
|
|
|
public $tmpInputPath; // Path to directory containing TMP file |
31
|
|
|
public $inputFilePath; // Path to input file locally |
32
|
|
|
public $s3Utils; // Used to manipulate S3. Download/Upload |
33
|
|
|
|
34
|
|
|
// Constants |
35
|
|
|
const TMP_FOLDER_FAIL = "TMP_FOLDER_FAIL"; |
36
|
|
|
const UNKOWN_INPUT_TYPE = "UNKOWN_INPUT_TYPE"; |
37
|
|
|
|
38
|
|
|
// JSON checks |
39
|
|
|
const FORMAT_INVALID = "FORMAT_INVALID"; |
40
|
|
|
|
41
|
|
|
// Types |
42
|
|
|
const VIDEO = "VIDEO"; |
43
|
|
|
const THUMB = "THUMB"; |
44
|
|
|
const AUDIO = "AUDIO"; |
45
|
|
|
const DOC = "DOC"; |
46
|
|
|
const IMAGE = "IMAGE"; |
47
|
|
|
|
48
|
|
|
// XXX Use EFS for storage |
49
|
|
|
// Nico: Expensive though. |
50
|
|
|
// This is where we store temporary files for transcoding for now |
51
|
|
|
// Make sure your partition is big enough! |
52
|
|
|
const TMP_FOLDER = "/tmp/CloudTranscode/"; |
53
|
|
|
|
54
|
|
|
public function __construct($client = null, $params, $debug, $cpeLogger) |
55
|
|
|
{ |
56
|
|
|
parent::__construct($client, $params, $debug, $cpeLogger); |
57
|
|
|
|
58
|
|
|
// S3 utils |
59
|
|
|
$this->s3Utils = new S3Utils($this->cpeLogger); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Perform the activity |
63
|
|
|
public function process($task) |
64
|
|
|
{ |
65
|
|
|
// Use workflowID to generate a unique TMP folder localy. |
66
|
|
|
$this->tmpInputPath = self::TMP_FOLDER |
67
|
|
|
. $this->logKey."/" |
68
|
|
|
. "input"; |
69
|
|
|
|
70
|
|
|
$inputFileInfo = null; |
71
|
|
|
if (isset($this->input->{'input_asset'}->{'file'})) { |
72
|
|
|
$this->input->{'input_asset'}->{'file'} = ltrim($this->input->{'input_asset'}->{'file'}, "/"); |
73
|
|
|
$inputFileInfo = pathinfo($this->input->{'input_asset'}->{'file'}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Create the tmp folder if doesn't exist |
77
|
|
|
if (!file_exists($this->tmpInputPath)) |
78
|
|
|
{ |
79
|
|
View Code Duplication |
if ($this->debug) |
|
|
|
|
80
|
|
|
$this->cpeLogger->logOut("DEBUG", basename(__FILE__), |
81
|
|
|
"Creating TMP input folder '".$this->tmpInputPath."'", |
82
|
|
|
$this->logKey); |
83
|
|
|
|
84
|
|
|
if (!mkdir($this->tmpInputPath, 0750, true)) |
85
|
|
|
throw new CpeSdk\CpeException( |
86
|
|
|
"Unable to create temporary folder '$this->tmpInputPath' !", |
87
|
|
|
self::TMP_FOLDER_FAIL |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->inputFilePath = null; |
92
|
|
|
if (isset($this->input->{'input_asset'}->{'http'})) |
93
|
|
|
{ |
94
|
|
|
// Pad HTTP input so it is cached in case of full encodes |
95
|
|
|
$this->inputFilePath = 'cache:' . $this->input->{'input_asset'}->{'http'}; |
96
|
|
|
} |
97
|
|
|
else if (isset($this->input->{'input_asset'}->{'bucket'}) && |
98
|
|
|
isset($this->input->{'input_asset'}->{'file'})) |
99
|
|
|
{ |
100
|
|
|
// Download input file and store it in TMP folder |
101
|
|
|
$saveFileTo = $this->tmpInputPath."/".$inputFileInfo['basename']; |
102
|
|
|
$this->inputFilePath = $this->getFileToProcess( |
103
|
|
|
$task, |
104
|
|
|
$this->input->{'input_asset'}->{'bucket'}, |
105
|
|
|
$this->input->{'input_asset'}->{'file'}, |
106
|
|
|
$saveFileTo |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Custom code for Cloud Transcode |
113
|
|
|
*/ |
114
|
|
|
|
115
|
|
|
// Create TMP folder and download file to process |
116
|
|
|
public function getFileToProcess($task, $inputBuket, $inputFile, $saveFileTo) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
// Get file from S3 or local copy if any |
119
|
|
|
$this->cpeLogger->logOut("INFO", |
120
|
|
|
basename(__FILE__), |
121
|
|
|
"Downloading '$inputBuket/$inputFile' to '$saveFileTo' ...", |
122
|
|
|
$this->logKey); |
123
|
|
|
|
124
|
|
|
// Use the S3 utils to initiate the download |
125
|
|
|
$s3Output = $this->s3Utils->get_file_from_s3( |
126
|
|
|
$inputBuket, |
127
|
|
|
$inputFile, |
128
|
|
|
$saveFileTo, |
129
|
|
|
array($this, "activityHeartbeat"), |
130
|
|
|
null, |
131
|
|
|
$this->logKey |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$this->cpeLogger->logOut("INFO", basename(__FILE__), |
135
|
|
|
$s3Output['msg'], |
136
|
|
|
$this->logKey); |
137
|
|
|
|
138
|
|
|
$this->cpeLogger->logOut("INFO", basename(__FILE__), |
139
|
|
|
"Input file successfully downloaded into local TMP folder '$saveFileTo' !", |
140
|
|
|
$this->logKey); |
141
|
|
|
|
142
|
|
|
return $saveFileTo; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
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.