about summary refs log tree commit diff homepage
path: root/misc.go
diff options
context:
space:
mode:
Diffstat (limited to 'misc.go')
-rw-r--r--misc.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/misc.go b/misc.go
new file mode 100644
index 0000000..700256c
--- /dev/null
+++ b/misc.go
@@ -0,0 +1,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/")
+}