This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
/
upload-file.js
100 lines (87 loc) · 2.75 KB
/
upload-file.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
module.exports = {
friendlyName: 'Upload File',
description: 'Uploads a file to a given website, overwriting if it already exists.',
extendedDescription: '',
inputs: {
deploymentUser: {
description: 'The deployment username for the website',
example: 'johndoe',
required: true
},
deploymentPassword: {
description: 'The deployment password for the website',
example: 'p@ssw0rd',
required: true
},
fileLocation: {
description: 'The path to the file that should be uploaded',
example: '',
required: true
},
remotePath: {
description: 'The path the file should be uploaded to, including the filename',
example: '/site/wwwroot/myfile.zip',
required: true
},
website: {
description: 'The name of the website the file should be uploaded to',
example: 'mysite',
required: true
}
},
defaultExit: 'success',
exits: {
error: {
description: 'Unexpected error occurred.'
},
success: {
description: 'Done.'
}
},
fn: function (inputs,exits) {
var child_process = require('child_process');
var readline = require('readline');
var cliPath = require('path').resolve(__dirname, '../node_modules/azure-cli/bin/azure');
var _ = require('lodash');
var request = require('request');
var fs = require('fs');
var checkForError = function (response) {
response = (response[0] && response[0].headers) ? response[0] : response;
if (response.headers && response.headers['content-type'] && response.headers['content-type'] === 'text/html') {
// 'Azure returned text/html (which it shouldn't), checking for errors
if (response.body && response.body.indexOf('401 - Unauthorized') > -1) {
return 'Invalid Credentials: The Azure Website rejected the given username or password.';
}
}
return false;
}
var fileStream = fs.createReadStream(inputs.fileLocation);
var targetUrl = 'https://' + inputs.website + '.scm.azurewebsites.net/api/vfs/' + inputs.remotePath;
request.del(targetUrl, {
'auth': {
'user': inputs.deploymentUser,
'pass': inputs.deploymentPassword
}
}, function (err) {
if (err) {
return exits.error(err);
}
fileStream.pipe(request.put(targetUrl, {
'auth': {
'user': inputs.deploymentUser,
'pass': inputs.deploymentPassword
}
},
function (err, result) {
if (err) {
return exits.error(err);
}
errorCheck = checkForError(result);
if (errorCheck) {
return exits.error(errorCheck);
}
return exits.success('Done');
}));
});
},
};