-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (157 loc) · 5.61 KB
/
index.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
var AWS = require('aws-sdk');
var JobResult = require('./job-result');
var JobError = require('./job-error');
/**
* Middleware to create and read jobs
*/
class Encoder{
/**
* Creates an instance
* @param {string} AWS_ACCESS_KEY_ID
* @param {string} AWS_SECRET_ACCESS_KEY
* @param {string} awsRegion
* @param {string} pipelineId
*/
constructor(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
awsRegion,
pipelineId,
inputBucketName,
outputBucketName){
/**
* The Id of the pipeline that you want Elastic Transcoder to use for transcoding. The pipeline determines several settings, including the Amazon S3 bucket from which Elastic Transcoder gets the files to transcode and the bucket into which Elastic Transcoder puts the transcoded files.
* @type {string}
*/
this.pipelineId = pipelineId;
/**
* Region for transcoder and S3 storage
*/
this.awsRegion = awsRegion;
/** Source files */
this.inputBucketName = inputBucketName;
/**
* Output bucket name, according a pipeline
*/
this.outputBucketName = outputBucketName;
/**
* Constructs a service interface object. Each API operation is exposed as a function on service.
*/
this.etr = new AWS.ElasticTranscoder({
region: awsRegion,
apiVersion: '2012-09-25',
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
maxRetries: 3
});
/** Access denied */
this.errAccessDenied = new JobError('AccessDeniedException');
/** Unexpected: need to handle all errors */
this.errUnexpected = new JobError('Unexpected');
/**
* AWS error for job processing. Set this error after error response.
* message = errorCode
* {@link http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/error-handling.html}
*/
this.errAws = null;
}
/**
* Create a job: If you have specified more than one output for your jobs (for example, one output for the Kindle Fire and another output for the Apple iPhone 4s), you currently must use the Elastic Transcoder API to list the jobs (as opposed to the AWS Console).
* @param {string} srcFile The name of the file to transcode.
* If the file name includes a prefix, such as cooking/lasagna.mpg, include the prefix in the key.
* If the file isn't in the specified bucket, Elastic Transcoder returns an error.
* @param {string} dstFile The name to assign to the transcoded file.
* If a file with the specified name already exists in the output bucket, the job fails.
* @param {string} presetId The Id of the preset to use for this job.
* The preset determines the audio, video, and thumbnail settings that Elastic Transcoder uses for transcoding.
* @param {Encoder~jobCallback} next
* @returns {Object} JSON data that includes the values that you specified plus information about the job that is created
*/
createJob(srcFile, dstFile, presetId, next) {
// A section of the request body that provides information about the file that is being transcoded.
var inputParams = {
Key: srcFile
};
var outputParams = {
Key: dstFile,
PresetId: presetId
};
// The value, if any, that you want Elastic Transcoder to prepend to the names of all files that this job creates, including output files, thumbnails, and playlists.
// OutputKeyPrefix: 'converted/'
this.etr.createJob({
PipelineId: this.pipelineId,
Input: inputParams,
Output: outputParams
}, (err, data) => this.handleJob(err, data, next));
}
/**
* @param {string} jobId
* @param {Encoder~jobCallback} next
* @return {Object} Detailed information about a job
*/
readJob(jobId, next){
this.etr.readJob({
Id: jobId
}, (err, data) => this.handleJob(err, data, next));
}
/**
* Handle result of operations
* @private
* @param {Object} err This attribute is only filled if a service or networking error occurs.
* {@link http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Response.html#error-property}
* @param {Object} data Response data, usually { Job: {} }
* @param {Encoder~jobCallback} next
*/
handleJob(err, data, next){
if (err){
console.log(err.statusCode, err.message);
if (err.code === 'AccessDeniedException') {
next(this.errAccessDenied);
return;
}
next(this.errUnexpected);
return;
}
var job = data.Job;
// it a necessary field
if (!job){
next(this.errUnexpected);
return;
}
// one job per time
var output = job.Output;
if (!output){
next(this.errUnexpected);
return;
}
// the same as job.Status
if (output.Status === 'Error') {
var detail = output.StatusDetail;
if (!detail){
next(this.errUnexpected);
return;
}
// http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/error-handling.html
var errCode = parseInt(detail.split(' ')[0]);
if (errCode) {
this.errAws = new JobError(errCode);
next(this.errAws);
return;
}
// if (errCode === 3002) {
// next(this.errDuplicate);
// return;
// }
next(this.errUnexpected);
return;
}
next(null, new JobResult(data.Job, 's3', this.awsRegion, this.outputBucketName));
}
/**
* Encoder callback
* @callback Encoder~jobCallback
* @param {JobError} err One of the encoder errors, e.g: errAccessDenied
* @param {JobResult} jobResult
*/
}
module.exports = Encoder;