2023-12-01 23:47:00 +00:00
|
|
|
package files
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-09 22:24:48 +00:00
|
|
|
"mime"
|
2025-01-21 14:02:43 +00:00
|
|
|
"path/filepath"
|
2019-01-05 22:44:33 +00:00
|
|
|
"regexp"
|
2023-07-21 22:41:24 +00:00
|
|
|
"strconv"
|
2023-08-05 14:58:06 +00:00
|
|
|
"strings"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
2023-07-21 22:41:24 +00:00
|
|
|
var typeRegexp = regexp.MustCompile(`type:(\S+)`)
|
2023-10-18 15:32:22 +00:00
|
|
|
var AllFiletypeOptions = []string{
|
|
|
|
"image",
|
|
|
|
"audio",
|
|
|
|
"archive",
|
|
|
|
"video",
|
|
|
|
"doc",
|
2023-12-01 23:47:00 +00:00
|
|
|
"text",
|
2023-10-18 15:32:22 +00:00
|
|
|
}
|
2024-11-26 17:21:41 +00:00
|
|
|
|
|
|
|
// Document file extensions
|
2023-07-04 23:55:15 +00:00
|
|
|
var documentTypes = []string{
|
2024-11-26 17:21:41 +00:00
|
|
|
// Common Document Formats
|
|
|
|
".doc", ".docx", // Microsoft Word
|
|
|
|
".pdf", // Portable Document Format
|
|
|
|
".odt", // OpenDocument Text
|
|
|
|
".rtf", // Rich Text Format
|
2024-12-17 00:01:55 +00:00
|
|
|
".conf",
|
|
|
|
".bash_history",
|
|
|
|
".gitignore",
|
|
|
|
".htpasswd",
|
|
|
|
".profile",
|
|
|
|
".dockerignore",
|
|
|
|
".editorconfig",
|
2024-11-26 17:21:41 +00:00
|
|
|
|
|
|
|
// Presentation Formats
|
|
|
|
".ppt", ".pptx", // Microsoft PowerPoint
|
|
|
|
".odp", // OpenDocument Presentation
|
|
|
|
|
2024-12-17 00:01:55 +00:00
|
|
|
// google docs
|
|
|
|
".gdoc",
|
|
|
|
|
|
|
|
// google sheet
|
|
|
|
".gsheet",
|
|
|
|
|
2024-11-26 17:21:41 +00:00
|
|
|
// Spreadsheet Formats
|
|
|
|
".xls", ".xlsx", // Microsoft Excel
|
|
|
|
".ods", // OpenDocument Spreadsheet
|
|
|
|
|
|
|
|
// Other Document Formats
|
|
|
|
".epub", // Electronic Publication
|
|
|
|
".mobi", // Amazon Kindle
|
|
|
|
".fb2", // FictionBook
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
2024-11-26 17:21:41 +00:00
|
|
|
|
2025-01-21 14:02:43 +00:00
|
|
|
var onlyOfficeSupported = []string{
|
|
|
|
".doc", ".docm", ".docx", ".dot", ".dotm", ".dotx", ".epub",
|
|
|
|
".fb2", ".fodt", ".htm", ".html", ".mht", ".mhtml", ".odt",
|
|
|
|
".ott", ".rtf", ".stw", ".sxw", ".txt", ".wps", ".wpt", ".xml",
|
|
|
|
".csv", ".et", ".ett", ".fods", ".ods", ".ots", ".sxc", ".xls",
|
|
|
|
".xlsb", ".xlsm", ".xlsx", ".xlt", ".xltm", ".xltx", ".dps",
|
|
|
|
".dpt", ".fodp", ".odp", ".otp", ".pot", ".potm", ".potx",
|
|
|
|
".pps", ".ppsm", ".ppsx", ".ppt", ".pptm", ".pptx", ".sxi",
|
|
|
|
".djvu", ".docxf", ".oform", ".oxps", ".pdf", ".xps",
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:21:41 +00:00
|
|
|
// Text-based file extensions
|
2023-12-01 23:47:00 +00:00
|
|
|
var textTypes = []string{
|
2024-11-26 17:21:41 +00:00
|
|
|
// Common Text Formats
|
|
|
|
".txt",
|
|
|
|
".md", // Markdown
|
|
|
|
|
|
|
|
// Scripting and Programming Languages
|
|
|
|
".sh", // Bash script
|
|
|
|
".py", // Python
|
|
|
|
".js", // JavaScript
|
|
|
|
".ts", // TypeScript
|
|
|
|
".php", // PHP
|
|
|
|
".rb", // Ruby
|
|
|
|
".go", // Go
|
|
|
|
".java", // Java
|
|
|
|
".c", ".cpp", // C/C++
|
|
|
|
".cs", // C#
|
|
|
|
".swift", // Swift
|
|
|
|
|
|
|
|
// Configuration Files
|
|
|
|
".yaml", ".yml", // YAML
|
|
|
|
".json", // JSON
|
|
|
|
".xml", // XML
|
|
|
|
".ini", // INI
|
|
|
|
".toml", // TOML
|
|
|
|
".cfg", // Configuration file
|
|
|
|
|
|
|
|
// Other Text-Based Formats
|
|
|
|
".css", // Cascading Style Sheets
|
|
|
|
".html", ".htm", // HyperText Markup Language
|
|
|
|
".sql", // SQL
|
|
|
|
".csv", // Comma-Separated Values
|
|
|
|
".tsv", // Tab-Separated Values
|
|
|
|
".log", // Log file
|
|
|
|
".bat", // Batch file
|
|
|
|
".ps1", // PowerShell script
|
|
|
|
".tex", // LaTeX
|
|
|
|
".bib", // BibTeX
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
2024-02-10 00:13:02 +00:00
|
|
|
|
2024-11-26 17:21:41 +00:00
|
|
|
// Compressed file extensions
|
2023-07-04 23:55:15 +00:00
|
|
|
var compressedFile = []string{
|
|
|
|
".7z",
|
|
|
|
".rar",
|
|
|
|
".zip",
|
|
|
|
".tar",
|
2023-12-01 23:47:00 +00:00
|
|
|
".gz",
|
|
|
|
".xz",
|
2024-11-26 17:21:41 +00:00
|
|
|
".bz2",
|
|
|
|
".tgz", // tar.gz
|
|
|
|
".tbz2", // tar.bz2
|
|
|
|
".lzma",
|
|
|
|
".lz4",
|
|
|
|
".zstd",
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 16:30:41 +00:00
|
|
|
type SearchOptions struct {
|
2023-08-12 19:41:59 +00:00
|
|
|
Conditions map[string]bool
|
|
|
|
LargerThan int
|
|
|
|
SmallerThan int
|
|
|
|
Terms []string
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:01:55 +00:00
|
|
|
func extendedMimeTypeCheck(extension string) string {
|
|
|
|
if isDoc(extension) {
|
|
|
|
return "application/document"
|
|
|
|
}
|
|
|
|
if isText(extension) {
|
|
|
|
return "text/plain"
|
|
|
|
}
|
|
|
|
return "blob"
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:21:41 +00:00
|
|
|
func ParseSearch(value string) SearchOptions {
|
|
|
|
opts := SearchOptions{
|
2023-08-05 14:58:06 +00:00
|
|
|
Conditions: map[string]bool{
|
2023-07-04 23:55:15 +00:00
|
|
|
"exact": strings.Contains(value, "case:exact"),
|
|
|
|
},
|
2023-08-05 14:58:06 +00:00
|
|
|
Terms: []string{},
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removes the options from the value
|
2023-07-04 23:55:15 +00:00
|
|
|
value = strings.Replace(value, "case:exact", "", -1)
|
2019-01-05 22:44:33 +00:00
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
|
|
|
types := typeRegexp.FindAllStringSubmatch(value, -1)
|
2023-07-04 23:55:15 +00:00
|
|
|
for _, filterType := range types {
|
|
|
|
if len(filterType) == 1 {
|
2019-01-05 22:44:33 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-07-21 22:41:24 +00:00
|
|
|
filter := filterType[1]
|
|
|
|
switch filter {
|
2023-08-05 14:58:06 +00:00
|
|
|
case "image":
|
|
|
|
opts.Conditions["image"] = true
|
|
|
|
case "audio", "music":
|
|
|
|
opts.Conditions["audio"] = true
|
|
|
|
case "video":
|
|
|
|
opts.Conditions["video"] = true
|
|
|
|
case "doc":
|
|
|
|
opts.Conditions["doc"] = true
|
|
|
|
case "archive":
|
|
|
|
opts.Conditions["archive"] = true
|
|
|
|
case "folder":
|
|
|
|
opts.Conditions["dir"] = true
|
|
|
|
case "file":
|
|
|
|
opts.Conditions["dir"] = false
|
2023-07-21 22:41:24 +00:00
|
|
|
}
|
|
|
|
if len(filter) < 8 {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-12 19:41:59 +00:00
|
|
|
if strings.HasPrefix(filter, "largerThan=") {
|
2023-07-21 22:41:24 +00:00
|
|
|
opts.Conditions["larger"] = true
|
2023-08-12 19:41:59 +00:00
|
|
|
size := strings.TrimPrefix(filter, "largerThan=")
|
|
|
|
opts.LargerThan = updateSize(size)
|
2023-07-21 22:41:24 +00:00
|
|
|
}
|
2023-08-12 19:41:59 +00:00
|
|
|
if strings.HasPrefix(filter, "smallerThan=") {
|
2024-10-07 22:44:53 +00:00
|
|
|
opts.Conditions["smaller"] = true
|
2023-08-12 19:41:59 +00:00
|
|
|
size := strings.TrimPrefix(filter, "smallerThan=")
|
|
|
|
opts.SmallerThan = updateSize(size)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(types) > 0 {
|
2023-08-12 16:30:41 +00:00
|
|
|
// Remove the fields from the search value
|
|
|
|
value = typeRegexp.ReplaceAllString(value, "")
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" {
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the value starts with " and finishes what that character, we will
|
|
|
|
// only search for that term
|
|
|
|
if value[0] == '"' && value[len(value)-1] == '"' {
|
|
|
|
unique := strings.TrimPrefix(value, "\"")
|
|
|
|
unique = strings.TrimSuffix(unique, "\"")
|
|
|
|
|
|
|
|
opts.Terms = []string{unique}
|
|
|
|
return opts
|
|
|
|
}
|
2023-08-05 14:58:06 +00:00
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
opts.Terms = strings.Split(value, "|")
|
2019-01-05 22:44:33 +00:00
|
|
|
return opts
|
|
|
|
}
|
2023-07-21 22:41:24 +00:00
|
|
|
|
|
|
|
func toInt(str string) int {
|
|
|
|
val, err := strconv.Atoi(str)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSize(given string) int {
|
|
|
|
size := toInt(given)
|
|
|
|
if size == 0 {
|
|
|
|
return 100
|
|
|
|
} else {
|
|
|
|
return size
|
|
|
|
}
|
2023-08-05 14:58:06 +00:00
|
|
|
}
|
2023-10-09 22:24:48 +00:00
|
|
|
|
|
|
|
func IsMatchingType(extension string, matchType string) bool {
|
|
|
|
mimetype := mime.TypeByExtension(extension)
|
|
|
|
if strings.HasPrefix(mimetype, matchType) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch matchType {
|
|
|
|
case "doc":
|
|
|
|
return isDoc(extension)
|
2023-12-01 23:47:00 +00:00
|
|
|
case "text":
|
|
|
|
return isText(extension)
|
2023-10-09 22:24:48 +00:00
|
|
|
case "archive":
|
|
|
|
return isArchive(extension)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:47:00 +00:00
|
|
|
func isText(extension string) bool {
|
|
|
|
for _, typefile := range textTypes {
|
|
|
|
if extension == typefile {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-09 22:24:48 +00:00
|
|
|
func isDoc(extension string) bool {
|
|
|
|
for _, typefile := range documentTypes {
|
|
|
|
if extension == typefile {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isArchive(extension string) bool {
|
|
|
|
for _, typefile := range compressedFile {
|
|
|
|
if extension == typefile {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2025-01-21 14:02:43 +00:00
|
|
|
|
|
|
|
func isOnlyOffice(name string) bool {
|
|
|
|
extention := filepath.Ext(name)
|
|
|
|
for _, typefile := range onlyOfficeSupported {
|
|
|
|
if extention == typefile {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|