summary refs log tree commit diff homepage
path: root/misc.go
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-15 15:02:43 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-15 15:55:37 +0900
commitb6f13b126acb62f036fa508cfc05120a718aea16 (patch)
tree547adfe8f39273c32a0a14c35fc961f4c02775a6 /misc.go
parent9654504997ab1d62f118f1fe896257f3011cdb57 (diff)
downloadphylactery-b6f13b126acb62f036fa508cfc05120a718aea16.tar.gz
Implement Atom feed
Implements: https://todo.sr.ht/~cnx/phylactery/2
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/")
+}