Successfully extracted an image

This commit is contained in:
Quentin 2021-04-26 11:05:31 +02:00
parent 67b86ae256
commit aca98e2d2a
Signed by: quentin
GPG key ID: A98E9B769E4FF428
4 changed files with 61 additions and 3 deletions

View file

@ -2,6 +2,8 @@ package main
import( import(
"log" "log"
"os"
"io"
) )
func cmdHead(config *configCollect) { func cmdHead(config *configCollect) {
@ -44,3 +46,20 @@ func cmdInfo(config *configCollect) {
en.Parse() en.Parse()
log.Println(en.Elem.String()) log.Println(en.Elem.String())
} }
func cmdCpFile(config *configCollect) {
en := NewEntryFileNode(config)
en.Parse()
file, err := os.Create(config.Dest)
if err != nil { log.Fatal(err) }
written, err := io.Copy(file, en)
if err != nil { log.Fatal(err) }
if uint64(written) != en.Elem.FileSize {
log.Fatal(written, "bytes written,", en.Elem.FileSize, "bytes expected")
}
}
func cmdCpDir(config *configCollect) {
}

View file

@ -10,5 +10,5 @@ type configCollect struct {
DirId string `docopt:"--dir"` DirId string `docopt:"--dir"`
FileId string `docopt:"--file"` FileId string `docopt:"--file"`
RepoId string `docopt:"<repoid>"` RepoId string `docopt:"<repoid>"`
S3Url string `docopt:"<s3url>"` Dest string `docopt:"<dest>"`
} }

34
fs.go
View file

@ -5,6 +5,7 @@ import(
"fmt" "fmt"
"log" "log"
"os" "os"
"io"
"io/ioutil" "io/ioutil"
"encoding/json" "encoding/json"
"syscall" "syscall"
@ -54,6 +55,10 @@ type FileNode struct {
Ent *DirEnt Ent *DirEnt
Elem FileElem Elem FileElem
AbsolutePath string AbsolutePath string
ReadBytes uint64
CurrentBlock *os.File
RemainingBlocks []string
} }
func (fe* FileElem) String() string { func (fe* FileElem) String() string {
@ -163,6 +168,35 @@ func (fn* FileNode) Parse() {
jdec := json.NewDecoder(zfile) jdec := json.NewDecoder(zfile)
err = jdec.Decode(&fn.Elem) err = jdec.Decode(&fn.Elem)
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
fn.RemainingBlocks = fn.Elem.BlkIds
}
func (fn* FileNode) Read(p []byte) (n int, err error) {
if fn.CurrentBlock == nil && len(fn.RemainingBlocks) == 0 {
return 0, io.EOF
}
if fn.CurrentBlock == nil {
blockId := fn.RemainingBlocks[0]
fn.RemainingBlocks = fn.RemainingBlocks[1:]
path := filepath.Join(fn.Config.Storage, "blocks", fn.Config.RepoId, blockId[:2], blockId[2:])
fn.CurrentBlock, err = os.Open(path)
if err != nil {
return 0, err
}
}
n, err = fn.CurrentBlock.Read(p)
fn.ReadBytes += uint64(n)
if err == io.EOF {
err = nil
fn.CurrentBlock.Close()
fn.CurrentBlock = nil
}
return n, err
} }
func (dn* DirNode) Children() ([]*DirNode, []*FileNode) { func (dn* DirNode) Children() ([]*DirNode, []*FileNode) {

View file

@ -11,8 +11,8 @@ func main() {
Usage: Usage:
seafile_recovery [--storage=<sto>] head <repoid> seafile_recovery [--storage=<sto>] head <repoid>
seafile_recovery [--storage=<sto>] ls <repoid> (--dir=<dirid> | --file=<fileid>) seafile_recovery [--storage=<sto>] ls <repoid> (--dir=<dirid> | --file=<fileid>)
seafile_recovery [--storage=<sto>] cp <repoid> (--dir=<dirid> | --file=<fileid>) seafile_recovery [--storage=<sto>] cp <repoid> (--dir=<dirid> | --file=<fileid>) <dest>
seafile_recovery [--storage=<sto>] s3 <repoid> (--dir=<dirid> | --file=<pathid>) <s3url> seafile_recovery [--storage=<sto>] s3 <repoid> (--dir=<dirid> | --file=<pathid>) <dest>
seafile_recovery (-h | --help) seafile_recovery (-h | --help)
Options: Options:
@ -45,6 +45,11 @@ Options:
if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) }
if !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) } if !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) }
if len(config.DirId) > 0 {
cmdCpDir(config)
} else {
cmdCpFile(config)
}
} else if config.S3 { } else if config.S3 {
if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) }
if !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) } if !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) }