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(
"log"
"os"
"io"
)
func cmdHead(config *configCollect) {
@ -44,3 +46,20 @@ func cmdInfo(config *configCollect) {
en.Parse()
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"`
FileId string `docopt:"--file"`
RepoId string `docopt:"<repoid>"`
S3Url string `docopt:"<s3url>"`
Dest string `docopt:"<dest>"`
}

34
fs.go
View File

@ -5,6 +5,7 @@ import(
"fmt"
"log"
"os"
"io"
"io/ioutil"
"encoding/json"
"syscall"
@ -54,6 +55,10 @@ type FileNode struct {
Ent *DirEnt
Elem FileElem
AbsolutePath string
ReadBytes uint64
CurrentBlock *os.File
RemainingBlocks []string
}
func (fe* FileElem) String() string {
@ -163,6 +168,35 @@ func (fn* FileNode) Parse() {
jdec := json.NewDecoder(zfile)
err = jdec.Decode(&fn.Elem)
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) {

View File

@ -11,8 +11,8 @@ func main() {
Usage:
seafile_recovery [--storage=<sto>] head <repoid>
seafile_recovery [--storage=<sto>] ls <repoid> (--dir=<dirid> | --file=<fileid>)
seafile_recovery [--storage=<sto>] cp <repoid> (--dir=<dirid> | --file=<fileid>)
seafile_recovery [--storage=<sto>] s3 <repoid> (--dir=<dirid> | --file=<pathid>) <s3url>
seafile_recovery [--storage=<sto>] cp <repoid> (--dir=<dirid> | --file=<fileid>) <dest>
seafile_recovery [--storage=<sto>] s3 <repoid> (--dir=<dirid> | --file=<pathid>) <dest>
seafile_recovery (-h | --help)
Options:
@ -45,6 +45,11 @@ Options:
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 len(config.DirId) > 0 {
cmdCpDir(config)
} else {
cmdCpFile(config)
}
} else if config.S3 {
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) }