aboutsummaryrefslogtreecommitdiffstats
path: root/tmp.go
blob: e57e199971f714630ce75d0a3f471ddf0a80fb21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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)
		}
	}
}