diff --git a/cmd/list_devices.go b/cmd/list_devices.go index 0bde919..d76a7c1 100644 --- a/cmd/list_devices.go +++ b/cmd/list_devices.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "fmt" "github.com/erdaltsksn/cui" @@ -24,7 +25,8 @@ var listDevicesCmd = &cobra.Command{ color.Cyan(fmt.Sprintf("\t🎥 %v (%v)\n", partition.Device, utils.CameraGuess(partition.Device))) } - networkDevices, err := gopro.GetGoProNetworkAddresses() + ctx := context.Background() + networkDevices, err := gopro.GetGoProNetworkAddresses(ctx) if err != nil { cui.Error(err.Error()) } diff --git a/pkg/dji/location.go b/pkg/dji/location.go index 426932d..024791d 100644 --- a/pkg/dji/location.go +++ b/pkg/dji/location.go @@ -1,7 +1,10 @@ package dji import ( + "bufio" + "io" "io/ioutil" + "os" "regexp" "strconv" "strings" @@ -15,13 +18,20 @@ type LatLongPair struct { Indicator string } -var allDrones = map[string]LatLongPair{ - "NewMavics": { +type ACType int + +const ( + NewAircraft ACType = iota + OldAircraft +) + +var allDrones = map[ACType]LatLongPair{ + NewAircraft: { Latitude: regexp.MustCompile(`\[latitude[ ]?: ([+-]?(\d+\.?\d*)|(\.\d+))\]`), Longitude: regexp.MustCompile(`\[long[t]?itude[ ]?: ([+-]?(\d+\.?\d*)|(\.\d+))\]`), // DJI and their typos... }, - "OldMavics": { + OldAircraft: { Latitude: regexp.MustCompile(`GPS[ ]?\(([+-]?(\d+\.?\d*)|(\.\d+))`), Longitude: regexp.MustCompile(`,[ ]?([+-]?(\d+\.?\d*)|(\.\d+)),[ ]?\d+\)`), }, @@ -41,7 +51,14 @@ func (LocationService) GetLocation(path string) (*utils.Location, error) { } func fromSRT(srtPath string) (*utils.Location, error) { - content, err := ioutil.ReadFile(strings.Replace(srtPath, ".MP4", ".SRT", -1)) + fs, err := os.Open(strings.Replace(srtPath, ".MP4", ".SRT", -1)) + if err != nil { + return nil, err + } + defer fs.Close() + reader := bufio.NewReader(fs) + limitedSizeReader := io.LimitReader(reader, 2048) + content, err := ioutil.ReadAll(limitedSizeReader) if err != nil { return nil, err } @@ -51,13 +68,9 @@ func fromSRT(srtPath string) (*utils.Location, error) { for _, drone := range allDrones { latMatches := drone.Latitude.FindAllStringSubmatch(string(content), -1) - if len(latMatches) == 0 { - continue - } - lonMatches := drone.Longitude.FindAllStringSubmatch(string(content), -1) - if len(lonMatches) == 0 { + if len(lonMatches) == 0 || len(latMatches) == 0 { continue } diff --git a/pkg/dji/location_test.go b/pkg/dji/location_test.go index 5765fc7..84749e9 100644 --- a/pkg/dji/location_test.go +++ b/pkg/dji/location_test.go @@ -24,6 +24,7 @@ func TestParseSRT(t *testing.T) { "Mini_SE.SRT": 1470, "mavic_mini.SRT": 145, "p4_rtk.SRT": 250, + "mavic_air2.srt": 290, } fs, err := gitfs.New(ctx, "github.com/JuanIrache/DJI_SRT_Parser/samples", gitfs.OptGlob("*.srt", "*.SRT")) diff --git a/pkg/generic/generic.go b/pkg/generic/generic.go deleted file mode 100644 index 5c12933..0000000 --- a/pkg/generic/generic.go +++ /dev/null @@ -1,10 +0,0 @@ -package generic - -import ( - mErrors "github.com/konradit/mmt/pkg/errors" - "github.com/konradit/mmt/pkg/utils" -) - -func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string) (*utils.Result, error) { - return nil, mErrors.ErrGeneric -} diff --git a/pkg/gopro/connect.go b/pkg/gopro/connect.go index bb4a34d..b8d92e9 100644 --- a/pkg/gopro/connect.go +++ b/pkg/gopro/connect.go @@ -3,6 +3,7 @@ package gopro /* GoPro Connect - API exposed over USB Ethernet */ import ( + "context" "encoding/json" "fmt" "log" @@ -31,12 +32,13 @@ var ( func handleKill() { c := make(chan os.Signal, 2) + ctx := context.Background() signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c color.Red("\nKilling program, exiting Turbo mode.") if gpTurbo { - if err := caller(ipAddress, "gp/gpTurbo?p=0", nil); err != nil { + if err := caller(ctx, ipAddress, "gp/gpTurbo?p=0", nil); err != nil { color.Red("Could not exit turbo mode") } } @@ -44,11 +46,12 @@ func handleKill() { }() } -func caller(ip, path string, object interface{}) error { +func caller(ctx context.Context, ip, path string, object interface{}) error { req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/%s", ip, path), nil) if err != nil { return err } + req = req.WithContext(ctx) resp, err := utils.Client.Do(req) if err != nil { return err @@ -81,12 +84,15 @@ func head(path string) (int, error) { return length, nil } -func GetGoProNetworkAddresses() ([]ConnectDevice, error) { +func GetGoProNetworkAddresses(ctx context.Context) ([]ConnectDevice, error) { + ctx, cancelCtx := context.WithTimeout(ctx, 2*time.Second) + defer cancelCtx() ipsFound := []ConnectDevice{} ifaces, err := net.Interfaces() if err != nil { return ipsFound, err } + for _, i := range ifaces { addrs, err := i.Addrs() if err != nil { @@ -98,7 +104,7 @@ func GetGoProNetworkAddresses() ([]ConnectDevice, error) { if r.MatchString(ipv4Addr.String()) { correctIP := ipv4Addr.String()[:len(ipv4Addr.String())-1] + "1" gpInfo := &cameraInfo{} - err := caller(correctIP, "gp/gpControl/info", gpInfo) + err := caller(ctx, correctIP, "gp/gpControl/info", gpInfo) if err != nil { continue } @@ -113,8 +119,9 @@ func GetGoProNetworkAddresses() ([]ConnectDevice, error) { } func GetMediaList(in string) (*MediaList, error) { + ctx := context.Background() gpMediaList := &MediaList{} - err := caller(in, "gp/gpMediaList", gpMediaList) + err := caller(ctx, in, "gp/gpMediaList", gpMediaList) if err != nil { return nil, err } @@ -147,7 +154,8 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) { return nil, mErrors.ErrInvalidSuppliedData(ipAddress) } gpInfo := &cameraInfo{} - err := caller(params.Input, "gp/gpControl/info", gpInfo) + ctx := context.Background() + err := caller(ctx, params.Input, "gp/gpControl/info", gpInfo) if err != nil { return nil, mErrors.ErrNotFound("Connect camera: " + params.Input) } @@ -169,7 +177,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) { // activate turbo if gpTurbo { - err = caller(params.Input, "gp/gpTurbo?p=1", nil) + err = caller(ctx, params.Input, "gp/gpTurbo?p=1", nil) if err != nil { color.Red("Error activating Turbo! Download speeds will be much slower") } @@ -255,7 +263,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) { finalPath := utils.GetOrder(params.Sort, locationService, filepath.Join(unsorted, origFilename), params.Output, mediaDate, cameraName) gpFileInfo := &goProMediaMetadata{} - err = caller(in, fmt.Sprintf("gp/gpMediaMetadata?p=%s/%s&t=v4info", folder, origFilename), gpFileInfo) + err = caller(ctx, in, fmt.Sprintf("gp/gpMediaMetadata?p=%s/%s&t=v4info", folder, origFilename), gpFileInfo) if err != nil { inlineCounter.SetFailure(err, origFilename) return @@ -401,7 +409,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) { filename := fmt.Sprintf("%s%04d.JPG", filebaseroot, i) gpFileInfo := &goProMediaMetadata{} - err = caller(params.Input, fmt.Sprintf("gp/gpMediaMetadata?p=%s/%s&t=v4info", folder.D, filename), gpFileInfo) + err = caller(ctx, params.Input, fmt.Sprintf("gp/gpMediaMetadata?p=%s/%s&t=v4info", folder.D, filename), gpFileInfo) if err != nil { log.Fatal(err.Error()) } @@ -449,7 +457,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) { wg.Wait() progressBar.Shutdown() if gpTurbo { - if err := caller(params.Input, "gp/gpTurbo?p=0", nil); err != nil { + if err := caller(ctx, params.Input, "gp/gpTurbo?p=0", nil); err != nil { color.Red("Could not exit turbo mode") } } diff --git a/pkg/gopro/detect.go b/pkg/gopro/detect.go index 565be2c..d9b913e 100644 --- a/pkg/gopro/detect.go +++ b/pkg/gopro/detect.go @@ -1,6 +1,8 @@ package gopro import ( + "context" + mErrors "github.com/konradit/mmt/pkg/errors" "github.com/konradit/mmt/pkg/utils" "github.com/shirou/gopsutil/disk" @@ -17,7 +19,8 @@ func Detect() (string, utils.ConnectionType, error) { } } - networkDevices, err := GetGoProNetworkAddresses() + ctx := context.Background() + networkDevices, err := GetGoProNetworkAddresses(ctx) if err != nil { return "", "", err } diff --git a/pkg/gopro/mp4parse.go b/pkg/gopro/mp4parse.go index 050c51f..feca6b5 100644 --- a/pkg/gopro/mp4parse.go +++ b/pkg/gopro/mp4parse.go @@ -25,6 +25,8 @@ func (*HMMT) GetType() mp4.BoxType { } func (h *HMMT) GetFieldLength(name string, ctx mp4.Context) uint { + _ = name + _ = ctx return uint(h.Count) } diff --git a/pkg/utils/cameras.go b/pkg/utils/cameras.go index 83c69ba..ee73670 100644 --- a/pkg/utils/cameras.go +++ b/pkg/utils/cameras.go @@ -58,6 +58,11 @@ func CameraGuess(input string) string { if err == nil { return Insta360.ToString() } + + _, err = os.Stat(filepath.Join(input, "MISC", "GIS", "dji.gis")) + if err == nil { + return DJI.ToString() + } return "" } @@ -199,10 +204,7 @@ func DownloadFile(filepath string, url string, progressbar *mpb.Bar) error { // Close the file without defer so it can happen before Rename() out.Close() - if err = os.Rename(filepath+".tmp", filepath); err != nil { - return err - } - return nil + return os.Rename(filepath+".tmp", filepath) } func Unzip(src string, dest string) error { diff --git a/pkg/utils/client.go b/pkg/utils/client.go index cc8f92b..bc60085 100644 --- a/pkg/utils/client.go +++ b/pkg/utils/client.go @@ -9,7 +9,7 @@ import ( func timeoutFromConfig() int { key := "network_timeout" - viper.SetDefault(key, 10) + viper.SetDefault(key, 4) return viper.GetInt(key) }