|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "mime/multipart" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strconv" |
| 14 | + "time" |
| 15 | + |
| 16 | + "golang.design/x/clipboard" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + expires := flag.Int("expires", 0, "set expiration time for the uploaded file in hours") |
| 21 | + secret := flag.Bool("secret", false, "enable secret mode") |
| 22 | + flag.Usage = func() { |
| 23 | + fmt.Fprintf(os.Stderr, "Usage: [-expires <hours>] [-secret] <filePath>\n") |
| 24 | + flag.PrintDefaults() |
| 25 | + fmt.Print("\n") |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + flag.Parse() |
| 29 | + |
| 30 | + args := flag.Args() |
| 31 | + if len(args) < 1 { |
| 32 | + flag.Usage() |
| 33 | + } |
| 34 | + filePath := args[0] |
| 35 | + |
| 36 | + file, err := os.Open(filePath) |
| 37 | + if err != nil { |
| 38 | + fmt.Println("Failed to open file:", err) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + defer file.Close() |
| 42 | + |
| 43 | + body := &bytes.Buffer{} |
| 44 | + writer := multipart.NewWriter(body) |
| 45 | + part, err := writer.CreateFormFile("file", filepath.Base(filePath)) |
| 46 | + if err != nil { |
| 47 | + fmt.Println("Failed to create form file:", err) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + io.Copy(part, file) |
| 51 | + |
| 52 | + if *expires > 0 { |
| 53 | + expiration := time.Now().Add(time.Duration(*expires) * time.Hour) |
| 54 | + fmt.Println("Expires:", expiration.Format(time.RFC3339)) |
| 55 | + fmt.Println() |
| 56 | + writer.WriteField("expires", strconv.Itoa(*expires)) |
| 57 | + } |
| 58 | + |
| 59 | + if *secret { |
| 60 | + writer.WriteField("secret", "true") |
| 61 | + } |
| 62 | + |
| 63 | + err = writer.Close() |
| 64 | + if err != nil { |
| 65 | + fmt.Println("Failed to close multipart writer:", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + |
| 69 | + request, err := http.NewRequest("POST", "https://0x0.st", body) |
| 70 | + if err != nil { |
| 71 | + fmt.Println("Failed to create HTTP request:", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + request.Header.Set("Content-Type", writer.FormDataContentType()) |
| 75 | + |
| 76 | + client := &http.Client{} |
| 77 | + response, err := client.Do(request) |
| 78 | + if err != nil { |
| 79 | + fmt.Println("Failed to send HTTP request:", err) |
| 80 | + os.Exit(1) |
| 81 | + } |
| 82 | + defer response.Body.Close() |
| 83 | + |
| 84 | + responseBody, err := ioutil.ReadAll(response.Body) |
| 85 | + if err != nil { |
| 86 | + fmt.Println("Failed to read response body:", err) |
| 87 | + os.Exit(1) |
| 88 | + } |
| 89 | + |
| 90 | + if responseBody != nil { |
| 91 | + fmt.Println(string(responseBody)) |
| 92 | + clipboard.Write(clipboard.FmtText, []byte(string(responseBody))) |
| 93 | + } |
| 94 | +} |
0 commit comments