-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidm.go
181 lines (162 loc) · 4.39 KB
/
idm.go
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
package idm
import (
wapi "github.com/iamacarpet/go-win64api"
"os"
"path/filepath"
"strings"
"time"
)
// GetIDMPath returns the IDM installation path if idm is installed
// Returns empty string if idm isn't installed or failed to get the applications list
// Call VerifyIDM first if you're not sure if IDM is installed
func GetIDMPath() string {
if !idmInstallationChecked {
_, _ = VerifyIDM()
}
return idmPath
}
func SetIDMPath(path string) error {
if _, err := os.Stat(path); err != nil {
return IDMPathError
}
idmPath = path
idmInstalled = true
idmInstallationChecked = true
return nil
}
// VerifyIDM verifies the IDM installation on the machine
func VerifyIDM() (bool, error) {
if idmInstallationChecked {
return idmInstalled, nil
}
// Get the list of installed applications
sw, err := wapi.InstalledSoftwareList()
if err != nil {
idmInstalled = true
idmInstallationChecked = true
return false, &ApplicationsListError{err: err}
}
for _, s := range sw {
if s.Name() == "Internet Download Manager" {
idmInstalled = true
idmInstallationChecked = true
// IDM installation path is not available as s.InstallLocation
// So replace Uninstall.exe with IDMs' standard name "IDMan.exe" to get the exe path
idmPath = strings.Replace(s.UninstallString, "Uninstall.exe", "IDMan.exe", 1)
break
}
}
return idmInstalled, nil
}
// StartMainQueue starts the main queue in IDM
func StartMainQueue() error {
installed, err := VerifyIDM()
if err != nil {
return err
}
if !installed {
return IDMNotInstalledError
}
return mainQueueStart()
}
// AddToQueue adds the given url to IDM queue
// File download won't be started
func AddToQueue(url string) error {
if !isUrl(url) {
return InValidURLError
}
return addToQueue(url)
}
// NewDownload creates a new download
func NewDownload(url string) (*Download, error) {
if !idmInstallationChecked {
exits, err := VerifyIDM()
if err != nil {
return nil, err
}
if !exits {
return nil, IDMNotInstalledError
}
}
if !isUrl(url) {
return nil, InValidURLError
}
download := Download{}
download.URL = url
return &download, nil
}
// AddToQueue adds specified file to download queue, but don't start downloading
// Calling Start() after calling this method won't download the queued download (starts a new download in IDM).
func (d *Download) AddToQueue() error {
return AddToQueue(d.URL)
}
// Silent sets the /n param
// turns on the silent mode when IDM doesn't ask any questions
func (d *Download) Silent() *Download {
d.SilentMode = true
return d
}
// QuitAfterFinish sets the /q param
// IDM will exit after the successful downloading. This parameter works only for the first copy
func (d *Download) QuitAfterFinish() *Download {
d.Quit = true
return d
}
// HangUpAfterFinish sets the /h param
// IDM will hang up your connection after the successful downloading
func (d *Download) HangUpAfterFinish() *Download {
d.Quit = true
return d
}
// SetFilePath sets the /p param
// Defines the local path where to save the file
func (d *Download) SetFilePath(path string) *Download {
d.Path = path
return d
}
// SetFilePath sets the /f param
// Defines local file name to save the file
func (d *Download) SetFileName(name string) *Download {
d.FileName = name
return d
}
// Start starts the file download in IDM
func (d *Download) Start() error {
if !isUrl(d.URL) {
return InValidURLError
}
return startDownload(d.buildArgs())
}
// VerifyDownload waits for download to be completed
// Only works if you specified the download path
// NOTE: File path must've been provided and file name must either be provided or be present in url header
func (d *Download) VerifyDownload(timeout time.Duration) error {
if d.Path == "" {
return DownloadFilePathNotProvidedError
}
err := d.setFileName()
if err != nil {
return err
}
fullPath, err := d.GetFullPath()
if err != nil {
return err
}
err = waitForFileToAppear(fullPath, timeout)
return err
}
// GetFullPath returns the full path of the file
// File path must be provided using SetFilePath method
// If file name hasn't been set, it will try to get it from url header (returns error if not available in header)
func (d *Download) GetFullPath() (string, error) {
if d.Path == "" {
return "", DownloadFilePathNotProvidedError
}
if d.FileName == "" {
err := d.setFileName()
if err != nil {
return "", FileNameDetectionError
}
}
return filepath.Join(d.Path, d.FileName), nil
}