2019-08-31 14:14:28 +00:00
|
|
|
package file_picker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-04-17 12:26:15 +00:00
|
|
|
func fileFilter(method string, extensions []string, size int, multi bool) (string, error) {
|
2019-08-31 14:14:28 +00:00
|
|
|
switch method {
|
2020-04-27 21:25:57 +00:00
|
|
|
case "any":
|
2020-04-17 12:26:15 +00:00
|
|
|
if multi {
|
|
|
|
return "*", nil
|
|
|
|
}
|
2019-08-31 14:14:28 +00:00
|
|
|
return `"public.item"`, nil
|
2020-04-27 21:25:57 +00:00
|
|
|
case "image":
|
2020-04-17 12:26:15 +00:00
|
|
|
if multi {
|
|
|
|
return "jpg, jpeg, bmp, gif, png", nil
|
|
|
|
}
|
2019-08-31 14:14:28 +00:00
|
|
|
return `"public.image"`, nil
|
2020-04-27 21:25:57 +00:00
|
|
|
case "audio":
|
2020-04-17 12:26:15 +00:00
|
|
|
if multi {
|
|
|
|
return "mp3, wav, midi, ogg, aac", nil
|
|
|
|
}
|
2019-08-31 14:14:28 +00:00
|
|
|
return `"public.audio"`, nil
|
2020-04-27 21:25:57 +00:00
|
|
|
case "video":
|
2020-04-17 12:26:15 +00:00
|
|
|
if multi {
|
|
|
|
return "webm, mpeg, mkv, mp4, avi, mov, flv", nil
|
|
|
|
}
|
2019-08-31 14:14:28 +00:00
|
|
|
return `"public.movie"`, nil
|
2020-04-27 21:25:57 +00:00
|
|
|
case "media":
|
|
|
|
if multi {
|
|
|
|
return "webm, mpeg, mkv, mp4, avi, mov, flv, jpg, jpeg, bmp, gif, png", nil
|
|
|
|
}
|
|
|
|
return `"public.audiovisual-content"`, nil
|
|
|
|
case "custom":
|
2020-04-08 11:45:06 +00:00
|
|
|
var i int
|
|
|
|
var filters = ""
|
2020-04-17 12:26:15 +00:00
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
filters += extensions[i]
|
|
|
|
if i < size-1 {
|
|
|
|
filters += ", "
|
|
|
|
}
|
2019-08-31 14:14:28 +00:00
|
|
|
}
|
2020-04-08 11:45:06 +00:00
|
|
|
return filters, nil
|
|
|
|
default:
|
2019-08-31 14:14:28 +00:00
|
|
|
return "", errors.New("unknown method")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileDialog(title string, filter string) (string, error) {
|
|
|
|
osascript, err := exec.LookPath("osascript")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := exec.Command(osascript, "-e", `choose file of type {`+filter+`} with prompt "`+title+`"`).Output()
|
|
|
|
if err != nil {
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
fmt.Printf("miguelpruivo/plugins_flutter_file_picker/go: file dialog exited with code %d and output `%s`\n", exitError.ExitCode(), string(output))
|
|
|
|
return "", nil // user probably canceled or closed the selection window
|
|
|
|
}
|
|
|
|
return "", errors.Wrap(err, "failed to open file dialog")
|
|
|
|
}
|
|
|
|
|
|
|
|
trimmedOutput := strings.TrimSpace(string(output))
|
|
|
|
|
|
|
|
pathParts := strings.Split(trimmedOutput, ":")
|
|
|
|
path := string(filepath.Separator) + filepath.Join(pathParts[1:]...)
|
|
|
|
return path, nil
|
|
|
|
}
|
2020-06-17 21:13:44 +00:00
|
|
|
|
|
|
|
func dirDialog(title string) (string, error) {
|
2020-08-10 14:35:43 +00:00
|
|
|
osascript, err := exec.LookPath("osascript")
|
2020-06-17 21:13:44 +00:00
|
|
|
if err != nil {
|
2020-08-10 14:35:43 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := exec.Command(osascript, "-e", `choose folder with prompt "`+title+`"`).Output()
|
|
|
|
if err != nil {
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
fmt.Printf("miguelpruivo/plugins_flutter_file_picker/go: folder dialog exited with code %d and output `%s`\n", exitError.ExitCode(), string(output))
|
|
|
|
return "", nil // user probably canceled or closed the selection window
|
|
|
|
}
|
|
|
|
return "", errors.Wrap(err, "failed to open folder dialog")
|
2020-06-17 21:13:44 +00:00
|
|
|
}
|
2020-08-10 14:35:43 +00:00
|
|
|
|
|
|
|
trimmedOutput := strings.TrimSpace(string(output))
|
|
|
|
|
|
|
|
pathParts := strings.Split(trimmedOutput, ":")
|
|
|
|
path := string(filepath.Separator) + filepath.Join(pathParts[1:]...)
|
|
|
|
return path, nil
|
2020-06-17 21:13:44 +00:00
|
|
|
}
|