github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/dedup/helper_persistence.go (about) 1 package dedup 2 3 import ( 4 "bytes" 5 "os" 6 7 "github.com/pbberlin/tools/net/http/loghttp" 8 "github.com/pbberlin/tools/os/fsi" 9 "github.com/pbberlin/tools/os/fsi/common" 10 "github.com/pbberlin/tools/os/fsi/dsfs" 11 "github.com/pbberlin/tools/os/fsi/memfs" 12 "github.com/pbberlin/tools/os/fsi/osfs" 13 "golang.org/x/net/context" 14 "golang.org/x/net/html" 15 ) 16 17 var logDir = "c:/tmp/dedup/" 18 19 var memMapFileSys = memfs.New(memfs.DirSort("byDateDesc")) // package variable required as "persistence" 20 21 func GetFS(c context.Context, whichType int) (fs fsi.FileSystem) { 22 23 switch whichType { 24 case 0: 25 // re-instantiation would delete contents 26 fs = fsi.FileSystem(memMapFileSys) 27 case 1: 28 // must be re-instantiated for each request 29 dsFileSys := dsfs.New(dsfs.DirSort("byDateDesc"), dsfs.MountName("mntTest"), dsfs.AeContext(c)) 30 fs = fsi.FileSystem(dsFileSys) 31 case 2: 32 33 osFileSys := osfs.New(osfs.DirSort("byDateDesc")) 34 fs = fsi.FileSystem(osFileSys) 35 os.Chdir(logDir) 36 default: 37 panic("invalid whichType ") 38 } 39 40 return 41 } 42 43 func fileDump(lg loghttp.FuncBufUniv, fs fsi.FileSystem, 44 content interface{}, fNamer func() string, secondPart string) { 45 46 if fNamer != nil { 47 fn := fNamer() + secondPart 48 switch casted := content.(type) { 49 case *html.Node: 50 var b bytes.Buffer 51 err := html.Render(&b, casted) 52 lg(err) 53 if err != nil { 54 return 55 } 56 err = common.WriteFile(fs, fn, b.Bytes()) 57 lg(err) 58 case []byte: 59 err := common.WriteFile(fs, fn, casted) 60 lg(err) 61 } 62 63 } 64 65 }