Add fileUpload command (WIP)
This commit is contained in:
		@@ -110,6 +110,7 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor {
 | 
			
		||||
			"device":      autocompleteDevice,
 | 
			
		||||
			"verify":      autocompleteDevice,
 | 
			
		||||
			"unverify":    autocompleteDevice,
 | 
			
		||||
			"upload":      autocompleteFile,
 | 
			
		||||
			"import":      autocompleteFile,
 | 
			
		||||
			"export":      autocompleteFile,
 | 
			
		||||
			"export-room": autocompleteFile,
 | 
			
		||||
@@ -138,6 +139,7 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor {
 | 
			
		||||
			"react":      cmdReact,
 | 
			
		||||
			"edit":       cmdEdit,
 | 
			
		||||
			"download":   cmdDownload,
 | 
			
		||||
			"upload":	  cmdUpload,
 | 
			
		||||
			"open":       cmdOpen,
 | 
			
		||||
			"copy":       cmdCopy,
 | 
			
		||||
			"sendevent":  cmdSendEvent,
 | 
			
		||||
 
 | 
			
		||||
@@ -20,8 +20,10 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"math"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	dbg "runtime/debug"
 | 
			
		||||
@@ -32,6 +34,7 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
	"unicode"
 | 
			
		||||
 | 
			
		||||
	"github.com/gabriel-vasile/mimetype"
 | 
			
		||||
	"github.com/lucasb-eyer/go-colorful"
 | 
			
		||||
	"github.com/pkg/errors"
 | 
			
		||||
	"github.com/russross/blackfriday/v2"
 | 
			
		||||
@@ -176,10 +179,82 @@ func cmdRedact(cmd *Command) {
 | 
			
		||||
	cmd.Room.StartSelecting(SelectRedact, strings.Join(cmd.Args, " "))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func autocompleteFile(cmd *CommandAutocomplete) (completions []string, newText string) {
 | 
			
		||||
	inputPath, err := filepath.Abs(cmd.RawArgs)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var searchNamePrefix, searchDir string
 | 
			
		||||
	if strings.HasSuffix(cmd.RawArgs, "/") {
 | 
			
		||||
		searchDir = inputPath
 | 
			
		||||
	} else {
 | 
			
		||||
		searchNamePrefix = filepath.Base(inputPath)
 | 
			
		||||
		searchDir = filepath.Dir(inputPath)
 | 
			
		||||
	}
 | 
			
		||||
	files, err := ioutil.ReadDir(searchDir)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, file := range files {
 | 
			
		||||
		name := file.Name()
 | 
			
		||||
		if !strings.HasPrefix(name, searchNamePrefix) || (name[0] == '.' && searchNamePrefix == "") {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		fullPath := filepath.Join(searchDir, name)
 | 
			
		||||
		if file.IsDir() {
 | 
			
		||||
			fullPath += "/"
 | 
			
		||||
		}
 | 
			
		||||
		completions = append(completions, fullPath)
 | 
			
		||||
	}
 | 
			
		||||
	if len(completions) == 1 {
 | 
			
		||||
		newText = fmt.Sprintf("/%s %s", cmd.OrigCommand, completions[0])
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cmdDownload(cmd *Command) {
 | 
			
		||||
	cmd.Room.StartSelecting(SelectDownload, strings.Join(cmd.Args, " "))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cmdUpload(cmd *Command) {
 | 
			
		||||
	if len(cmd.Args) == 0 {
 | 
			
		||||
		cmd.Reply("Usage: /upload <file>")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	path, err := filepath.Abs(cmd.RawArgs)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		cmd.Reply("Failed to get absolute path: %v", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ftype, err := mimetype.DetectFile(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		cmd.Reply("Failed to get conetnt type: %v", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fi, err := os.Stat(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		cmd.Reply("Failed to stat file: %v", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	reader, err := os.Open(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		cmd.Reply("Failed to open file: %v", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cmd.Room.UploadMedia(mautrix.ReqUploadMedia{
 | 
			
		||||
		reader,
 | 
			
		||||
		fi.Size(),
 | 
			
		||||
		ftype.String(),
 | 
			
		||||
		filepath.Base(cmd.RawArgs),
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cmdOpen(cmd *Command) {
 | 
			
		||||
	cmd.Room.StartSelecting(SelectOpen, strings.Join(cmd.Args, " "))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -232,40 +232,6 @@ func cmdResetSession(cmd *Command) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func autocompleteFile(cmd *CommandAutocomplete) (completions []string, newText string) {
 | 
			
		||||
	inputPath, err := filepath.Abs(cmd.RawArgs)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var searchNamePrefix, searchDir string
 | 
			
		||||
	if strings.HasSuffix(cmd.RawArgs, "/") {
 | 
			
		||||
		searchDir = inputPath
 | 
			
		||||
	} else {
 | 
			
		||||
		searchNamePrefix = filepath.Base(inputPath)
 | 
			
		||||
		searchDir = filepath.Dir(inputPath)
 | 
			
		||||
	}
 | 
			
		||||
	files, err := ioutil.ReadDir(searchDir)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, file := range files {
 | 
			
		||||
		name := file.Name()
 | 
			
		||||
		if !strings.HasPrefix(name, searchNamePrefix) || (name[0] == '.' && searchNamePrefix == "") {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		fullPath := filepath.Join(searchDir, name)
 | 
			
		||||
		if file.IsDir() {
 | 
			
		||||
			fullPath += "/"
 | 
			
		||||
		}
 | 
			
		||||
		completions = append(completions, fullPath)
 | 
			
		||||
	}
 | 
			
		||||
	if len(completions) == 1 {
 | 
			
		||||
		newText = fmt.Sprintf("/%s %s", cmd.OrigCommand, completions[0])
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cmdImportKeys(cmd *Command) {
 | 
			
		||||
	path, err := filepath.Abs(cmd.RawArgs)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -22,10 +22,6 @@ func autocompleteDevice(cmd *CommandAutocomplete) ([]string, string) {
 | 
			
		||||
	return []string{}, ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func autocompleteFile(cmd *CommandAutocomplete) ([]string, string) {
 | 
			
		||||
	return []string{}, ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cmdNoCrypto(cmd *Command) {
 | 
			
		||||
	cmd.Reply("This gomuks was built without encryption support")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"maunium.net/go/gomuks/config"
 | 
			
		||||
	"maunium.net/go/gomuks/debug"
 | 
			
		||||
	"maunium.net/go/gomuks/interface"
 | 
			
		||||
	ifc "maunium.net/go/gomuks/interface"
 | 
			
		||||
	"maunium.net/go/gomuks/lib/open"
 | 
			
		||||
	"maunium.net/go/gomuks/matrix/muksevt"
 | 
			
		||||
	"maunium.net/go/gomuks/matrix/rooms"
 | 
			
		||||
@@ -606,8 +606,8 @@ func findWordToTabComplete(text string) string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	mentionMarkdown = "[%[1]s](https://matrix.to/#/%[2]s)"
 | 
			
		||||
	mentionHTML = `<a href="https://matrix.to/#/%[2]s">%[1]s</a>`
 | 
			
		||||
	mentionMarkdown  = "[%[1]s](https://matrix.to/#/%[2]s)"
 | 
			
		||||
	mentionHTML      = `<a href="https://matrix.to/#/%[2]s">%[1]s</a>`
 | 
			
		||||
	mentionPlaintext = "%[1]s"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -806,6 +806,20 @@ func (view *RoomView) SendMessageHTML(msgtype event.MessageType, text, html stri
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (view *RoomView) SendImage(body string, url id.ContentURI) {
 | 
			
		||||
	// evt := view.parent.matrix.SendImage(view.Room.ID, body, url)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (view *RoomView) UploadMedia(data mautrix.ReqUploadMedia) {
 | 
			
		||||
	contentUri, err := view.parent.matrix.UploadMedia(data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		view.AddServiceMessage(fmt.Sprintf("Failed to upload file: %v", err))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	view.AddServiceMessage(fmt.Sprintf("ContentURI: %v", contentUri))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (view *RoomView) MessageView() *MessageView {
 | 
			
		||||
	return view.content
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user