1
|
|
|
import * as upath from 'upath'; |
2
|
|
|
import { readFileSync } from 'fs'; |
3
|
|
|
import { Client } from 'scp2'; |
4
|
|
|
import Config from './Config'; |
5
|
|
|
import CLI from './CLI'; |
6
|
|
|
|
7
|
|
|
export default class Uploader { |
8
|
|
|
client: Client; |
9
|
|
|
|
10
|
|
|
constructor(private config: Config, private cli: CLI) {} |
11
|
|
|
|
12
|
|
|
connect(): Promise<string> { |
13
|
|
|
/// |
14
|
|
|
this.client = new Client({ |
15
|
|
|
port: this.config.port, |
16
|
|
|
host: this.config.host, |
17
|
|
|
username: this.config.username, |
18
|
|
|
password: this.config.password, |
19
|
|
|
// agentForward: true, |
20
|
|
|
privateKey: this.config.privateKey |
21
|
|
|
? readFileSync(upath.normalizeSafe(this.config.privateKey)).toString() |
22
|
|
|
: undefined, |
23
|
|
|
// debug: true |
24
|
|
|
}); |
25
|
|
|
//console.log(this.client); |
26
|
|
|
|
27
|
|
|
// Triggers the initial connection |
28
|
|
|
this.client.sftp((err, sftp) => { |
29
|
|
|
if (err) { |
30
|
|
|
console.log('There was a problem with connection'); |
31
|
|
|
} |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
return new Promise<string>((resolve, reject) => { |
35
|
|
|
this.client.on('ready', () => { |
36
|
|
|
resolve('connected'); |
37
|
|
|
}); |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
getRemotePath(path: string): string { |
42
|
|
|
let normalPath = upath.normalizeSafe(path); |
43
|
|
|
let normalLocalPath = upath.normalizeSafe(this.config.localPath); |
44
|
|
|
let remotePath = normalPath.replace( |
45
|
|
|
normalLocalPath, |
46
|
|
|
this.config.remotePath |
47
|
|
|
); |
48
|
|
|
let pathJoin = upath.join(this.config.remotePath, remotePath); |
49
|
|
|
return pathJoin; |
50
|
|
|
//console.log(pathJoin); |
51
|
|
|
//return upath.normalizeSafe(`${this.config.remotePath}/${remotePath}`); |
52
|
|
|
//return upath.normalizeSafe(remotePath); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
unlinkFile(fileName: string): Promise<string> { |
56
|
|
|
return new Promise<string>((resolve, reject) => { |
57
|
|
|
let remote = this.getRemotePath(fileName); |
58
|
|
|
this.client.sftp((err, sftp) => { |
59
|
|
|
if (err) { |
60
|
|
|
reject('SFTP cannot be created'); |
61
|
|
|
} else { |
62
|
|
|
sftp.unlink(remote, (err) => { |
63
|
|
|
if (err) { |
64
|
|
|
reject('File could not be deleted'); |
65
|
|
|
} else { |
66
|
|
|
resolve(remote); |
67
|
|
|
} |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
}); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
unlinkFolder(folderPath: string): Promise<string> { |
75
|
|
|
return new Promise<string>((resolve, reject) => { |
76
|
|
|
let remote = this.getRemotePath(folderPath); |
77
|
|
|
this.client.sftp((err, sftp) => { |
78
|
|
|
if (err) { |
79
|
|
|
reject('SFTP cannot be created'); |
80
|
|
|
} else { |
81
|
|
|
sftp.rmdir(remote, (err) => { |
82
|
|
|
if (err) { |
83
|
|
|
reject('Folder could not be deleted'); |
84
|
|
|
} else { |
85
|
|
|
resolve(remote); |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
}); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
uploadFile(fileName: string): Promise<string> { |
94
|
|
|
return new Promise<string>((resolve, reject) => { |
95
|
|
|
let remote = this.getRemotePath(fileName); |
96
|
|
|
///console.log(remote); |
97
|
|
|
|
98
|
|
|
// Client upload also creates the folder but creates it using local mode |
99
|
|
|
// in windows it might mean we won't have permissons to save the fileName |
100
|
|
|
// So I create the folder manually here to solve that issue. |
101
|
|
|
// Mode we set can be configured from the config file |
102
|
|
|
this.client.mkdir( |
103
|
|
|
upath.dirname(remote), |
104
|
|
|
{ mode: this.config.pathMode }, |
105
|
|
|
(err) => { |
106
|
|
|
if (err) { |
107
|
|
|
reject({ |
108
|
|
|
message: `Could not create ${upath.dirname(remote)}`, |
109
|
|
|
error: err, |
110
|
|
|
}); |
111
|
|
|
} else { |
112
|
|
|
// Uplad the file |
113
|
|
|
this.client.upload(fileName, remote, (err) => { |
114
|
|
|
if (err) { |
115
|
|
|
reject({ |
116
|
|
|
message: `Could not upload ${remote}`, |
117
|
|
|
error: err, |
118
|
|
|
}); |
119
|
|
|
} else { |
120
|
|
|
resolve(remote); |
121
|
|
|
} |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
); |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|