aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/upload/upload.go
blob: 15764319878f21d5e3e3078b11a5dff3aab450e6 (plain)
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
128
129
130
package upload

import (
	"archive/zip"
	"fmt"
	"io"
	"mime/multipart"
	"os"
	"path"
	"path/filepath"
	"strings"

	"github.com/LMBishop/scrapbook/pkg/config"
	"github.com/LMBishop/scrapbook/pkg/index"
)

func HandleUpload(siteName string, reader *multipart.Reader, index *index.SiteIndex) (string, error) {
	s := index.GetSite(siteName)
	if s == nil {
		return "", fmt.Errorf("no such site: %s", siteName)
	}

	if s.SiteConfig.Flags&config.FlagReadOnly != 0 {
		return "", fmt.Errorf("site is read only: %s", siteName)
	}

	temp, err := os.CreateTemp(os.TempDir(), "scrapbook")
	if err != nil {
		return "", fmt.Errorf("failed to create temporary file: %w", err)
	}
	defer func() {
		temp.Close()
		os.Remove(temp.Name())
	}()

	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		} else if err != nil {
			return "", fmt.Errorf("failed to read multipart stream: %w", err)
		}
		if part.FormName() == "upload" {
			io.Copy(temp, part)
		}
	}

	zipReader, err := zip.OpenReader(temp.Name())
	if err != nil {
		return "", fmt.Errorf("failed to open zip reader: %w", err)
	}
	defer zipReader.Close()

	version, err := s.CreateNewVersion()
	if err != nil {
		return "", fmt.Errorf("failed to create new version: %w", err)
	}
	versionDir := path.Join(s.Path, version)

	err = unzipSource(temp.Name(), versionDir)
	if err != nil {
		return "", fmt.Errorf("failed to unzip archive: %w", err)
	}

	err = s.UpdateVersion(version)
	if err != nil {
		return "", fmt.Errorf("failed to update version: %w", err)
	}

	return version, nil
}

// https://gosamples.dev/unzip-file/

func unzipSource(source, destination string) error {
	reader, err := zip.OpenReader(source)
	if err != nil {
		return err
	}
	defer reader.Close()

	destination, err = filepath.Abs(destination)
	if err != nil {
		return err
	}

	for _, f := range reader.File {
		err := unzipFile(f, destination)
		if err != nil {
			return err
		}
	}

	return nil
}

func unzipFile(f *zip.File, destination string) error {
	filePath := filepath.Join(destination, f.Name)
	if !strings.HasPrefix(filePath, filepath.Clean(destination)+string(os.PathSeparator)) {
		return fmt.Errorf("invalid file path: %s", filePath)
	}

	if f.FileInfo().IsDir() {
		if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
			return err
		}
		return nil
	}

	if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
		return err
	}

	destinationFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
	if err != nil {
		return err
	}
	defer destinationFile.Close()

	zippedFile, err := f.Open()
	if err != nil {
		return err
	}
	defer zippedFile.Close()

	if _, err := io.Copy(destinationFile, zippedFile); err != nil {
		return err
	}
	return nil
}