aboutsummaryrefslogblamecommitdiffstats
path: root/tmp.go
blob: e57e199971f714630ce75d0a3f471ddf0a80fb21 (plain) (tree)
1
2
3
4
5



             
                                                                  












                                                                                    






                                                                           































































                                                                                                       
                                    












                                                                                        
                                                  

                                                                             
                                      
 

                                                                                      








                                                                                                               
                                                         






                                                       
package main

import (
	"fmt"
	"log" // TODO: eventually change to slog and manage levels
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var COMPRESSED_EXTENSIONS = []string{".jpg", ".webm", ".ogg", ".gz", ".bz2", ".zip",
	".xz", ".odp", ".odt", ".ods", ".mp4", ".mp3",
	".png", ".pdf", ".apk", ".rpm", ".epub", ".mkv",
	".avi", ".jar", ".opus", ".ogv", ".flac", ".msi",
	".m4a"}

// THE VARIABLE URL_VARIANTS IS MEANT TO BE EDITED. Change various
// attributes to correspond to your situation.
// If new section is added (as here I have also 'tmp' for fedorapeople.org)
// and the script has a symlink named as the section (so I have tmp as
// a symlink to this script) it saves the file to the location defined in
// such section when called under the other name.
// So, `tmp filename` saves file filename to fedorapeople for me.
var URL_VARIANTS = map[string]map[string]string{
	"wotan": {
		"base_url":   "wotan:Export/",
		"target_url": "https://w3.suse.de/~mcepl/",
	},
	"tmp": {
		"base_url":   "fedorapeople.org:public_html/tmp/",
		"target_url": "https://mcepl.fedorapeople.org/tmp/",
		// "shorten_api": "http://is.gd/create.php?format=simple&url="
		"shorten_api": "https://da.gd/s?url=",
	},
}

func contains(slice []string, item string) bool {
	for _, a := range slice {
		if a == item {
			return true
		}
	}
	return false
}

func main() {
	if len(os.Args) < 2 {
		os.Exit(1)
	}

	cmdname := strings.ToLower(filepath.Base(os.Args[0]))
	config := URL_VARIANTS[cmdname]

	if _, err := os.Stat(os.Args[1]); os.IsNotExist(err) {
		os.Exit(1)
	}

	for _, processedFile := range os.Args[1:] {
		st, err := os.Stat(processedFile)
		if err != nil {
			log.Fatal(err)
		}
		fileExt := filepath.Ext(processedFile)
		log.Printf("size of %s is %d.", processedFile, st.Size())
		log.Printf("extension is %s", fileExt)

		var fname string
		var COMPRESSED bool

		if st.Size() > 1000000 && !contains(COMPRESSED_EXTENSIONS, strings.ToLower(fileExt)) {
			log.Printf("file %s should be compressed (%d b)", processedFile, st.Size())
			err := exec.Command("gzip", "--keep", "--best", processedFile).Run()
			if err != nil {
				log.Fatal("Compressing the file failed!")
			}
			fname = processedFile + ".gz"
			COMPRESSED = true
		} else {
			log.Printf("file %s should not be compressed (%d b)", processedFile, st.Size())
			fname = processedFile
			COMPRESSED = false
		}

		err = os.Chmod(fname, 0o644)
		if err != nil {
			log.Fatal(err)
		}
		// To make scp happy
		modifiedFname := strings.Replace(fname, ":", "_", 1)
		err = os.Rename(fname, modifiedFname)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Unshorten fname = %s", modifiedFname)

		_, err = exec.Command("scp", modifiedFname, config["base_url"]).Output()
		if err != nil {
			log.Fatal(err)
		}

		targetURL := config["target_url"] + filepath.Base(modifiedFname)
		// Make target_URL into a safe URL
		safeTargetURL := url.QueryEscape(targetURL)
		safeTargetURL = strings.Replace(safeTargetURL, "%3A", ":", 1)
		fmt.Println(targetURL)

		// curl -s 'http://is.gd/create.php?format=simple&url=www.example.com'
		// http://is.gd/MOgh5q
		if shortenAPI, ok := config["shorten_api"]; ok {
			shortenedURLBytes, err := exec.Command("curl", "-s", shortenAPI+safeTargetURL).Output()
			if err != nil {
				log.Fatal(err)
			}
			shortenedURL := strings.TrimSpace(string(shortenedURLBytes))
			fmt.Println(shortenedURL)
		}

		// The script should have no side-effects
		if COMPRESSED {
			os.Remove(modifiedFname)
		} else {
			os.Rename(modifiedFname, fname)
		}
	}
}