This commit is contained in:
shoopea 2019-06-11 23:38:12 +08:00
parent bc9e58588d
commit 7d4ae4b668

71
job.go
View File

@ -520,15 +520,70 @@ func jobBackupImport(j Job) {
err = json.Unmarshal(j.Payload, &p) err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobBackupImport : Unmarshal payload") logOnError(err, "jobBackupImport : Unmarshal payload")
if err == nil { resp, err := http.Get(p.URL)
m := TGCommand{ logOnError(err, "jobBackupImport : Get")
Type: commandReplyMsg, defer resp.Body.Close()
Text: "Backup imported.",
FromMsgID64: p.MsgID64, buf := new(bytes.Buffer)
FromChatID64: p.ChatID64, buf.ReadFrom(resp.Body)
}
TGCmdQueue <- m m := TGCommand{
Type: commandReplyMsg,
Text: "File downloaded.",
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
} }
TGCmdQueue <- m
z := buf.Bytes()
r := bytes.NewReader(z)
zr, err := zip.NewReader(r, int64(len(z)))
for _, f := range zr.File {
if strings.Compare(f.Name, "backup.json") == 0 {
rc, err := f.Open()
logOnError(err, "jobBackupImport : Open")
if err != nil {
return err
}
data, err := ioutil.ReadAll(rc)
logOnError(err, "jobBackupImport : ReadAll")
if err != nil {
return err
}
log.Printf("jobBackupImport : %d uncompressed bytes.\n", len(data))
rc.Close()
bkp := DataBackup{}
err = json.Unmarshal(data, &bkp)
logOnError(err, "jobBackupImport : Unmarshal")
if err != nil {
return err
}
for _, m := range bkp.Messages {
_, err = addObjMsg(m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
}
m := TGCommand{
Type: commandReplyMsg,
Text: "Backup restored.",
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
}
TGCmdQueue <- m
err = setJobDone(j.ID64)
logOnError(err, "jobBackupImport : setJobDone")
return
}
}
m := TGCommand{
Type: commandReplyMsg,
Text: "Not backup file found in archive.",
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
}
TGCmdQueue <- m
err = setJobDone(j.ID64) err = setJobDone(j.ID64)
logOnError(err, "jobBackupImport : setJobDone") logOnError(err, "jobBackupImport : setJobDone")