package pdfprocessor import ( "crypto/md5" "fmt" "io" "net/http" "os" "path/filepath" ) func DownloadPDF(url string, saveDir string) (string, string, error) { resp, err := http.Get(url) if err != nil { return "", "", fmt.Errorf("downloading PDF: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return "", "", fmt.Errorf("download failed with status: %d", resp.StatusCode) } data, err := io.ReadAll(resp.Body) if err != nil { return "", "", fmt.Errorf("reading PDF data: %w", err) } hash := fmt.Sprintf("%x", md5.Sum(data)) filename := fmt.Sprintf("%s.pdf", hash) filepath := filepath.Join(saveDir, filename) if err := os.WriteFile(filepath, data, 0644); err != nil { return "", "", fmt.Errorf("saving PDF: %w", err) } return filepath, hash, nil } func ComputeMD5(filePath string) (string, error) { f, err := os.Open(filePath) if err != nil { return "", err } defer f.Close() h := md5.New() if _, err := io.Copy(h, f); err != nil { return "", err } return fmt.Sprintf("%x", h.Sum(nil)), nil }