Something went wrong. Try again.
Supercharged auto-indexing of a filesystem for web hosting
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407package main
import ( "path/filepath" "database/sql"
_ "github.com/mattn/go-sqlite3" log "github.com/sirupsen/logrus"
"tangled.org/dominic-ricottone.com/autoindex/common")
const ( cmd_dropexporting = ` DROP TABLE IF EXISTS table_exporting;` cmd_dropcreators = ` DROP TABLE IF EXISTS table_creators;` cmd_droptags = ` DROP TABLE IF EXISTS table_tags;` schema_exporting = ` CREATE TABLE table_exporting ( Path TEXT PRIMARY KEY NOT NULL, Parent TEXT NOT NULL, Mode INTEGER NOT NULL, Title TEXT NOT NULL, Creators TEXT NOT NULL, Tags TEXT NOT NULL, Size INTEGER NOT NULL, Timestamp INTEGER NOT NULL, Children INTEGER NOT NULL, Link TEXT NOT NULL, Preview TEXT NOT NULL, Hidden BOOL NOT NULL DEFAULT 0 );` cmd_exporting = ` INSERT INTO table_exporting SELECT * FROM table_joined;` index_parents = ` CREATE INDEX idx_ExportingParents ON table_exporting(Parent);` schema_creators = ` CREATE TABLE table_creators ( Creator TEXT NOT NULL, Path TEXT NOT NULL );` schema_tags = ` CREATE TABLE table_tags ( Tag TEXT NOT NULL, Path TEXT NOT NULL );` template_insert_creator = ` INSERT INTO table_creators (Creator, Path) VALUES(?, ?);` template_insert_tag = ` INSERT INTO table_tags (Tag, Path) VALUES(?, ?);` command_folders = ` SELECT Path, Mode, Title, Creators, Tags FROM table_exporting WHERE Path != '.' AND Mode IN (11, 12, 13, 22, 23, 24, 25);` template_children_timestamp = ` SELECT Path, Mode, Title, Size, Timestamp, Children, Preview, Link FROM table_exporting WHERE Parent = ? AND Mode != 1 ORDER BY Timestamp DESC;` template_children_path = ` SELECT Path, Mode, Title, Size, Timestamp, Children, Preview, Link FROM table_exporting WHERE Parent = ? AND Mode != 1 ORDER BY Path ASC;` template_collect_taxonomies = ` SELECT DISTINCT Creators, Tags FROM table_exporting WHERE Path LIKE ?;` command_children = ` UPDATE table_exporting SET Hidden=1 WHERE EXISTS ( SELECT 1 FROM table_exporting AS parents WHERE parents.Mode IN (11,13,23,24,25) AND table_exporting.Parent != '.' AND table_exporting.Parent = parents.Path );` index_creators = ` CREATE INDEX idx_CreatorPaths ON table_creators(Path);` index_tags = ` CREATE INDEX idx_TagPaths ON table_tags(Path);` command_files = ` SELECT Path, Creators, Tags FROM table_exporting WHERE Mode IN (31, 32, 33, 34, 35) AND Hidden = 0;` command_shown = ` SELECT Path, Mode, Title, Size, Timestamp, Children, Preview, Link FROM table_exporting WHERE Hidden = 0 ORDER BY table_exporting.Timestamp DESC;` command_creators = ` SELECT table_creators.Creator, table_exporting.Path, table_exporting.Mode, table_exporting.Title, table_exporting.Size, table_exporting.Timestamp, table_exporting.Children, table_exporting.Preview, table_exporting.Link FROM table_exporting JOIN table_creators ON table_exporting.Path = table_creators.Path WHERE table_exporting.Hidden = 0 ORDER BY table_creators.Creator ASC, table_exporting.Timestamp DESC;` command_tags = ` SELECT table_tags.Tag, table_exporting.Path, table_exporting.Mode, table_exporting.Title, table_exporting.Size, table_exporting.Timestamp, table_exporting.Children, table_exporting.Preview, table_exporting.Link FROM table_exporting JOIN table_tags ON table_exporting.Path = table_tags.Path WHERE table_exporting.Hidden = 0 ORDER BY table_tags.Tag ASC, table_exporting.Timestamp DESC;`)
// Main routine. Export the database to the filesystem.func Export(db *sql.DB, w *ConfiguredWriter, preview bool) { tx, err := db.Begin() if err != nil { log.WithError(err).Fatal("cannot start transaction") } defer tx.Rollback()
if _, err = tx.Exec(cmd_dropexporting); err != nil { log.WithError(err).Fatal("error in transaction (dropping export table)") } if _, err = tx.Exec(schema_exporting); err != nil { log.WithError(err).Fatal("error in transaction (creating export table)") } if _, err = tx.Exec(cmd_exporting); err != nil { log.WithError(err).Fatal("error in transaction (populating export table)") } if _, err = tx.Exec(index_parents); err != nil { log.WithError(err).Fatal("error in transaction (indexing parents)") }
if _, err = tx.Exec(cmd_dropcreators); err != nil { log.WithError(err).Fatal("error in transaction (dropping creators table)") } if _, err = tx.Exec(schema_creators); err != nil { log.WithError(err).Fatal("error in transaction (creating creators table)") }
if _, err = tx.Exec(cmd_droptags); err != nil { log.WithError(err).Fatal("error in transaction (dropping tags table)") } if _, err = tx.Exec(schema_tags); err != nil { log.WithError(err).Fatal("error in transaction (creating tags table)") }
indexingCreators := len(w.Roots.Creators) > 0 indexingTags := len(w.Roots.Tags) > 0
log.Debug("exporting folders...")
// Anonymous function just to trigger `Close()` on time. func() { query, err := tx.Query(command_folders) defer query.Close() if err != nil { log.WithError(err).Fatal("error in transaction (querying directories)") }
for query.Next() { d := newDirectoryPage()
if err = query.Scan(&d.Path, &d.Mode, &d.Title, &d.creators, &d.tags); err != nil { log.WithError(err).Error("error scanning a query row") }
if indexingCreators || indexingTags { d.initTaxonomies(indexingCreators, indexingTags) taxonomiesUnder(tx, d) d.finiTaxonomies() }
// Add this directory to taxonomies, unless it is a namespace. if !common.IsNamespace(d.Mode) { // If not indexing creators, slice will be empty. for _, creator := range d.Creators { if _, err := tx.Exec(template_insert_creator, creator, d.Path); err != nil { log.WithError(err).Errorf("cannot add %s to the %s creator taxonomy", d.Path, creator) } }
// If not indexing tags, slice will be empty. for _, tag := range d.Tags { if _, err := tx.Exec(template_insert_tag, tag, d.Path); err != nil { log.WithError(err).Errorf("cannot add %s to the %s tag taxonomy", d.Path, tag) } } }
entriesUnder(tx, d)
d.write(w) } }()
if _, err = tx.Exec(command_children); err != nil { log.WithError(err).Fatal("error in transaction (hiding folder children)") } if _, err = tx.Exec(index_creators); err != nil { log.WithError(err).Fatal("error in transaction (indexing creators)") } if _, err = tx.Exec(index_tags); err != nil { log.WithError(err).Fatal("error in transaction (indexing tags)") }
if indexingCreators || indexingTags { func() { query, err := tx.Query(command_files) defer query.Close() if err != nil { log.WithError(err).Fatal("error in transaction (querying files)") }
var path string var creators string var tags string for query.Next() { if err = query.Scan(&path, &creators, &tags); err != nil { log.WithError(err).Error("error scanning a query row") }
if indexingCreators { for _, creator := range common.UnmarshalArray(creators) { if _, err := tx.Exec(template_insert_creator, creator, path); err != nil { log.WithError(err).Errorf("cannot add %s to the %s creator taxonomy", path, creator) } } }
if indexingTags { for _, tag := range common.UnmarshalArray(tags) { if _, err := tx.Exec(template_insert_tag, tag, path); err != nil { log.WithError(err).Errorf("cannot add %s to the %s tag taxonomy", path, tag) } } } } }() }
func() { query, err := tx.Query(command_shown) defer query.Close() if err != nil { log.WithError(err).Fatal("error in transaction (querying shown entries)") }
var list []entry var e entry
for query.Next() { if err = query.Scan(&e.LocalName, &e.Mode, &e.Title, &e.Size, &e.Timestamp, &e.Children, &e.Preview, &e.Link); err != nil { log.WithError(err).Error("error scanning a query row") } list = append(list, e) }
writeRoot(w, list, preview) }()
//func() { // query, err := tx.Query(command_files) // defer query.Close() // if err != nil { // log.WithError(err).Fatal("error in transaction (querying files)") // }
// var path string // var creators string // var tags string // // for query.Next() { // if err = query.Scan(&path, &creators, &tags); err != nil { // log.WithError(err).Error("error scanning a query row") // }
// if indexingCreators { // for _, creator := range common.UnmarshalArray(creators) { // if _, err := tx.Exec(template_insert_creator, creator, path); err != nil { // log.WithError(err).Errorf("cannot add %s to the %s creator taxonomy", path, creator) // } // } // }
// if indexingTags { // for _, tag := range common.UnmarshalArray(tags) { // if _, err := tx.Exec(template_insert_tag, tag, path); err != nil { // log.WithError(err).Errorf("cannot add %s to the %s tag taxonomy", path, tag) // } // } // } // } //}()
//func() { // query, err := tx.Query(command_shown) // defer query.Close() // if err != nil { // log.WithError(err).Fatal("error in transaction (querying shown entries)") // }
// root := newRootPage() // var e entry
// for query.Next() { // if err = query.Scan(&e.LocalName, &e.Mode, &e.Title, &e.Size, &e.Timestamp, &e.Children, &e.Preview, &e.Link); err != nil { // log.WithError(err).Error("error scanning a query row") // } // root.Entries = append(root.Entries, e) // }
// w.Reset() // root.write(w, preview) //}()
exportCreators(tx, w) exportTags(tx, w)
err = tx.Commit() if err != nil { log.WithError(err).Fatal("cannot complete transaction") }}
// Helper to collect taxonomies from all entries under a directory, and add to// the directory's own taxonomies.func taxonomiesUnder(tx *sql.Tx, dir *directoryPage) { like := dir.Path + string(filepath.Separator) + "%"
query, err := tx.Query(template_collect_taxonomies, like) defer query.Close() if err != nil { log.WithError(err).Fatalf("error in transaction (querying children of %s)", dir.Path) }
for query.Next() { var creators string var tags string
if err = query.Scan(&creators, &tags); err != nil { log.WithError(err).Error("error scanning a query row") }
dir.addTaxonomies(creators, tags) }}
// Helper to collect the entries under some directory.func entriesUnder(tx *sql.Tx, dir *directoryPage) { // Determine whether entries should be sorted by name or modtime. template := template_children_timestamp if common.IsOrdered(dir.Mode) { template = template_children_path }
query, err := tx.Query(template, dir.Path) defer query.Close() if err != nil { log.WithError(err).Fatalf("error in transaction (querying direct children of %s)", dir.Path) }
var e entry
for query.Next() { if err = query.Scan(&e.LocalName, &e.Mode, &e.Title, &e.Size, &e.Timestamp, &e.Children, &e.Preview, &e.Link); err != nil { log.WithError(err).Error("error scanning a query row") } dir.Entries = append(dir.Entries, e) }}