about summary refs log tree commit diff homepage
path: root/misc.go
blob: 700256c2144067592b3502890c569251d2f689e7 (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
// Miscellaneous utilities
// Copyright (C) 2022  Nguyễn Gia Phong
//
// This file is part of Phylactery.
//
// Phylactery is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Phylactery is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Phylactery.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"archive/zip"
	"net/http"
	"os"
	"strings"
)

// Function escape ensures that question marks in path
// are not recognized as URL parameters.
func escape(name string) string {
	return strings.ReplaceAll(name, "?", "%3f")
}

// Function find searches for the directory entry of given name.
func find(entries []os.DirEntry, name string) int {
	for i, entry := range entries {
		if entry.Name() == name {
			return i
		}
	}
	return -1
}

// Function isImageFile detects if given file is an image.
func isImageFile(file *zip.File) bool {
	image, _ := file.Open()
	defer image.Close()
	buf := make([]byte, 512)
	n, _ := image.Read(buf)
	mime := http.DetectContentType(buf[:n])
	return strings.HasPrefix(mime, "image/")
}