This repository has been archived on 2021-07-08. You can view files and clone it, but cannot push or open issues or pull requests.
opensend/internal/serialization/parameters.go

205 lines
6.4 KiB
Go
Raw Permalink Normal View History

/*
Copyright © 2021 Arsen Musayelyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-07-08 20:11:41 +00:00
package serialization
2020-12-31 07:54:18 +00:00
import (
"fmt"
2020-12-31 07:54:18 +00:00
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
2021-06-19 07:01:31 +00:00
"github.com/mholt/archiver/v3"
2021-06-19 07:01:31 +00:00
"github.com/pkg/browser"
"github.com/rs/zerolog/log"
"github.com/vmihailenco/msgpack/v5"
2020-12-31 07:54:18 +00:00
)
// Create config type to store action type and data
type Parameters struct {
ActionType string
ActionData string
}
// Instantiate and return a new Config struct
func NewParameters(actionType string, actionData string) *Parameters {
return &Parameters{ActionType: actionType, ActionData: actionData}
}
func (parameters *Parameters) Validate() {
if parameters.ActionType == "url" {
// Parse URL in parameters
urlParser, err := url.Parse(parameters.ActionData)
// If there was an error parsing
if err != nil {
// Alert user of invalid url
log.Fatal().Err(err).Msg("Invalid URL")
// If scheme is not detected
} else if urlParser.Scheme == "" {
// Alert user of invalid scheme
log.Fatal().Msg("Invalid URL scheme")
// If host is not detected
} else if urlParser.Host == "" {
// Alert user of invalid host
log.Fatal().Msg("Invalid URL host")
}
}
}
// Create config file
func (parameters *Parameters) CreateFile(dir string) {
// Use ConsoleWriter logger
// Create parameters file at given directory
2021-06-19 07:01:31 +00:00
configFile, err := os.Create(dir + "/parameters.msgpack")
2020-12-31 07:54:18 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error creating parameters file")
}
// Close parameters file at the end of this function
defer configFile.Close()
// Marshal given Parameters struct into a []byte
2021-06-19 07:01:31 +00:00
MessagePackData, err := msgpack.Marshal(parameters)
2020-12-31 07:54:18 +00:00
if err != nil {
2021-06-19 07:01:31 +00:00
log.Fatal().Err(err).Msg("Error encoding MessagePack")
2020-12-31 07:54:18 +00:00
}
// Write []byte to previously created parameters file
2021-06-19 07:01:31 +00:00
bytesWritten, err := configFile.Write(MessagePackData)
2020-12-31 07:54:18 +00:00
if err != nil {
2021-06-19 07:01:31 +00:00
log.Fatal().Err(err).Msg("Error writing MessagePack to file")
2020-12-31 07:54:18 +00:00
}
// Log bytes written
2021-06-19 07:01:31 +00:00
log.Info().Str("file", "parameters.msgpack").Msg("Wrote " + strconv.Itoa(bytesWritten) + " bytes")
2020-12-31 07:54:18 +00:00
}
// Collect all required files into given directory
func (parameters *Parameters) CollectFiles(dir string) {
// Use ConsoleWriter logger
// If action type is file
if parameters.ActionType == "file" {
// Open file path in parameters.ActionData
src, err := os.Open(parameters.ActionData)
if err != nil {
log.Fatal().Err(err).Msg("Error opening file from parameters")
}
// Close source file at the end of this function
defer src.Close()
// Create new file with the same name at given directory
dst, err := os.Create(dir + "/" + filepath.Base(parameters.ActionData))
if err != nil {
log.Fatal().Err(err).Msg("Error creating file")
}
// Close new file at the end of this function
defer dst.Close()
// Copy data from source file to destination file
_, err = io.Copy(dst, src)
if err != nil {
log.Fatal().Err(err).Msg("Error copying data to file")
}
// Replace file path in parameters.ActionData with file name
parameters.ActionData = filepath.Base(parameters.ActionData)
} else if parameters.ActionType == "dir" {
err := archiver.Archive([]string{parameters.ActionData}, dir+"/"+filepath.Base(parameters.ActionData)+".tar")
2020-12-31 07:54:18 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error creating tar archive")
}
// Set parameters data to base path for receiver
parameters.ActionData = filepath.Base(parameters.ActionData)
}
}
// Read config file at given file path
func (parameters *Parameters) ReadFile(filePath string) {
// Use ConsoleWriter logger
// Read file at filePath
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal().Err(err).Msg("Error reading parameters file")
}
2021-06-19 07:01:31 +00:00
// Unmarshal data from MessagePack into parameters struct
err = msgpack.Unmarshal(fileData, parameters)
2020-12-31 07:54:18 +00:00
if err != nil {
2021-06-19 07:01:31 +00:00
log.Fatal().Err(err).Msg("Error decoding MessagePack")
2020-12-31 07:54:18 +00:00
}
}
// Execute action specified in config
func (parameters *Parameters) ExecuteAction(srcDir string, destDir string) {
// Use ConsoleWriter logger
// If action is file
2021-01-03 05:44:09 +00:00
switch parameters.ActionType {
case "file":
2020-12-31 07:54:18 +00:00
// Open file from parameters at given directory
src, err := os.Open(srcDir + "/" + parameters.ActionData)
if err != nil {
log.Fatal().Err(err).Msg("Error reading file from parameters")
}
// Close source file at the end of this function
defer src.Close()
// Create file in user's Downloads directory
dst, err := os.Create(filepath.Clean(destDir) + "/" + parameters.ActionData)
if err != nil {
log.Fatal().Err(err).Msg("Error creating file")
}
// Close destination file at the end of this function
defer dst.Close()
// Copy data from source file to destination file
_, err = io.Copy(dst, src)
if err != nil {
log.Fatal().Err(err).Msg("Error copying data to file")
}
// If action is url
2021-01-03 05:44:09 +00:00
case "url":
2020-12-31 07:54:18 +00:00
// Parse received URL
urlParser, err := url.Parse(parameters.ActionData)
// If there was an error parsing
if err != nil {
// Alert user of invalid url
log.Fatal().Err(err).Msg("Invalid URL")
// If scheme is not detected
} else if urlParser.Scheme == "" {
// Alert user of invalid scheme
log.Fatal().Msg("Invalid URL scheme")
// If host is not detected
} else if urlParser.Host == "" {
// Alert user of invalid host
log.Fatal().Msg("Invalid URL host")
}
// Attempt to open URL in browser
err = browser.OpenURL(parameters.ActionData)
if err != nil {
log.Fatal().Err(err).Msg("Error opening browser")
}
// If action is dir
2021-01-03 05:44:09 +00:00
case "dir":
2020-12-31 07:54:18 +00:00
// Set destination directory to ~/Downloads/{dir name}
dstDir := filepath.Dir(filepath.Clean(destDir) + "/" + parameters.ActionData)
fmt.Println(dstDir)
err := archiver.Unarchive(srcDir+"/"+parameters.ActionData+".tar", dstDir)
2020-12-31 07:54:18 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error extracting tar archive")
2020-12-31 07:54:18 +00:00
}
// Loop to recursively unarchive tar file
// Catchall
2021-01-03 05:44:09 +00:00
default:
2020-12-31 07:54:18 +00:00
// Log unknown action type
log.Fatal().Msg("Unknown action type " + parameters.ActionType)
}
}