Skip to content

Commit 991b39c

Browse files
committed
Task download, cancel, unzipping
1 parent 304359d commit 991b39c

File tree

14 files changed

+567
-23
lines changed

14 files changed

+567
-23
lines changed

internal/cmd/root.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package cmd
1717

1818
import (
19-
"fmt"
2019
"mime"
2120
"os"
2221
"path/filepath"
@@ -33,6 +32,7 @@ import (
3332

3433
var outputPath string
3534
var nodeName string
35+
var force bool
3636

3737
var rootCmd = &cobra.Command{
3838
Use: "odm [flags] <images> [<gcp>] [args]",
@@ -45,6 +45,15 @@ var rootCmd = &cobra.Command{
4545
os.Exit(0)
4646
}
4747

48+
// Check output directory
49+
filesCount, err := fs.DirectoryFilesCount(outputPath)
50+
if err != nil {
51+
logger.Error(err)
52+
}
53+
if filesCount > 0 && !force {
54+
logger.Error(outputPath + " already exists (pass --force to override directory contents)")
55+
}
56+
4857
inputFiles, options := parseArgs(args)
4958
inputFiles = filterImagesAndText(inputFiles)
5059

@@ -74,7 +83,15 @@ var rootCmd = &cobra.Command{
7483
logger.Error(err)
7584
}
7685

77-
odm.Run(inputFiles, parseOptions(options, nodeOptions), *node)
86+
// Create output directory
87+
if !fs.IsDirectory(outputPath) {
88+
err = os.MkdirAll(outputPath, 0755)
89+
if err != nil {
90+
logger.Error(err)
91+
}
92+
}
93+
94+
odm.Run(inputFiles, parseOptions(options, nodeOptions), *node, outputPath)
7895
},
7996

8097
TraverseChildren: true,
@@ -95,6 +112,7 @@ func init() {
95112
rootCmd.PersistentFlags().BoolVarP(&logger.DebugFlag, "debug", "d", false, "show debug output")
96113
rootCmd.PersistentFlags().BoolVarP(&logger.QuietFlag, "quiet", "q", false, "suppress output")
97114

115+
rootCmd.Flags().BoolVarP(&force, "force", "f", false, "replace the contents of the output directory if it already exists")
98116
rootCmd.Flags().StringVarP(&outputPath, "output", "o", "./output", "directory where to store processing results")
99117
rootCmd.Flags().StringVarP(&nodeName, "node", "n", "default", "Processing node to use")
100118
rootCmd.Flags().SetInterspersed(false)
@@ -118,7 +136,6 @@ func parseArgs(args []string) ([]string, []string) {
118136
}
119137
}
120138
} else if fs.IsFile(arg) {
121-
fmt.Printf(arg)
122139
inputFiles = append(inputFiles, arg)
123140
} else {
124141
options = append(options, arg)

internal/config/auth.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
116
package config
217

318
import (

internal/config/configuration.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
116
package config
217

318
import (
@@ -38,8 +53,6 @@ func (c Configuration) Save() {
3853
type Configuration struct {
3954
Nodes map[string]odm.Node `json:"nodes"`
4055

41-
Tasks []odm.Task `json:"tasks"`
42-
4356
filePath string
4457
}
4558

internal/config/configuration_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
116
package config
217

318
import (

internal/config/publicnodes.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
116
package config
217

318
import (

internal/fs/fs.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
116
package fs
217

3-
import "os"
18+
import (
19+
"io/ioutil"
20+
"os"
21+
)
422

523
// FileExists checks if a file path exists
624
func FileExists(filePath string) (bool, error) {
@@ -33,3 +51,22 @@ func IsFile(path string) bool {
3351
}
3452
return !fileInfo.IsDir()
3553
}
54+
55+
// DirectoryFilesCount returns the number of files in a directory. If the dir
56+
// does not exists, it returns 0.
57+
func DirectoryFilesCount(dirPath string) (int, error) {
58+
if _, err := os.Stat(dirPath); err != nil {
59+
if os.IsNotExist(err) {
60+
return 0, nil
61+
} else {
62+
return -1, err
63+
}
64+
}
65+
66+
files, err := ioutil.ReadDir(dirPath)
67+
if err != nil {
68+
return -1, err
69+
}
70+
71+
return len(files), nil
72+
}

internal/fs/unzip.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
// Based on work published on https://golangcode.com/download-a-file-with-progress/
17+
18+
package fs
19+
20+
import (
21+
"archive/zip"
22+
"fmt"
23+
"io"
24+
"os"
25+
"path/filepath"
26+
"strings"
27+
"time"
28+
29+
"github.com/OpenDroneMap/CloudODM/internal/logger"
30+
"github.com/cheggaaa/pb"
31+
)
32+
33+
// Unzip decompresses a zip archive
34+
func Unzip(zipFilePath string, destDirectory string) ([]string, error) {
35+
var bar *pb.ProgressBar
36+
var filenames []string
37+
38+
r, err := zip.OpenReader(zipFilePath)
39+
if err != nil {
40+
return filenames, err
41+
}
42+
defer r.Close()
43+
44+
showProgress := !logger.QuietFlag && len(r.File) > 0
45+
if showProgress {
46+
bar = pb.New(len(r.File)).SetUnits(pb.U_NO).SetRefreshRate(time.Millisecond * 10)
47+
bar.Start()
48+
defer bar.Finish()
49+
}
50+
51+
for _, f := range r.File {
52+
53+
rc, err := f.Open()
54+
if err != nil {
55+
return filenames, err
56+
}
57+
defer rc.Close()
58+
59+
if showProgress {
60+
bar.Prefix("[" + f.Name + "]")
61+
}
62+
63+
// Store filename/path for returning and using later on
64+
fpath := filepath.Join(destDirectory, f.Name)
65+
66+
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
67+
if !strings.HasPrefix(fpath, filepath.Clean(destDirectory)+string(os.PathSeparator)) {
68+
return filenames, fmt.Errorf("%s: illegal file path", fpath)
69+
}
70+
71+
filenames = append(filenames, fpath)
72+
73+
if f.FileInfo().IsDir() {
74+
75+
// Make Folder
76+
os.MkdirAll(fpath, os.ModePerm)
77+
78+
} else {
79+
80+
// Make File
81+
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
82+
return filenames, err
83+
}
84+
85+
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
86+
if err != nil {
87+
return filenames, err
88+
}
89+
90+
_, err = io.Copy(outFile, rc)
91+
92+
// Close the file without defer to close before next iteration of loop
93+
outFile.Close()
94+
95+
if err != nil {
96+
return filenames, err
97+
}
98+
}
99+
100+
if showProgress {
101+
bar.Increment()
102+
}
103+
}
104+
105+
return filenames, nil
106+
}

internal/io/terminal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright © 2018 CloudODM Contributors
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
116
package io
217

318
import (

0 commit comments

Comments
 (0)