tagliatelle

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

include-admin-orphans.go (991B)


      1 package main
      2 
      3 import (
      4     "net/http"
      5     "os"
      6 )
      7 
      8 func getOrphanedFiles(uploadDir string) ([]string, error) {
      9 	diskFiles, err := getFilesOnDisk(uploadDir)
     10 	if err != nil {
     11 		return nil, err
     12 	}
     13 
     14 	dbFiles, err := getFilesInDB()
     15 	if err != nil {
     16 		return nil, err
     17 	}
     18 
     19 	var orphans []string
     20 	for _, f := range diskFiles {
     21 		if !dbFiles[f] {
     22 			orphans = append(orphans, f)
     23 		}
     24 	}
     25 	return orphans, nil
     26 }
     27 
     28 func orphansHandler(w http.ResponseWriter, r *http.Request) {
     29 	orphans, err := getOrphanedFiles(config.UploadDir)
     30 	if err != nil {
     31 		renderError(w, "Error reading orphaned files", http.StatusInternalServerError)
     32 		return
     33 	}
     34 
     35 	pageData := buildPageData("Orphaned Files", orphans)
     36 	renderTemplate(w, "orphans.html", pageData)
     37 }
     38 
     39 func getFilesOnDisk(uploadDir string) ([]string, error) {
     40 	entries, err := os.ReadDir(uploadDir)
     41 	if err != nil {
     42 		return nil, err
     43 	}
     44 	var files []string
     45 	for _, e := range entries {
     46 		if !e.IsDir() {
     47 			files = append(files, e.Name())
     48 		}
     49 	}
     50 	return files, nil
     51 }