Commit 66b49239 authored by Rahul A. Sutar's avatar Rahul A. Sutar
Browse files

Fixed download helper bug

1) Modified fileutil.go to return a status message.
2) Removed all  log.fatal
parent 4cda9273
Branches
Tags
1 merge request!18Fixed download helper bug
Showing with 15 additions and 14 deletions
......@@ -6,7 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
......@@ -16,13 +16,13 @@ import (
func createTempFile(partFilename string, fileBegin, fileEnd int) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, int64(fileBegin)); err != nil {
log.Fatal(err)
panic(err)
}
if err := binary.Write(buf, binary.LittleEndian, int64(fileEnd)); err != nil {
log.Fatal(err)
panic(err)
}
if err := ioutil.WriteFile(partFilename, buf.Bytes(), 0666); err != nil {
log.Fatal(err)
panic(err)
}
}
......@@ -47,7 +47,7 @@ func writeBytes(partFilename string, reader []byte, byteStart, byteEnd int) erro
func readHeader(partFilename string) (int, int) {
reader, err := ioutil.ReadFile(partFilename)
if err != nil {
log.Fatal(err)
panic(err)
}
header := reader[:16]
byteStart := int(binary.LittleEndian.Uint64(header[0:8])) + len(reader) - 16
......@@ -63,20 +63,20 @@ func mergeFiles(filename string, count int) {
file, err := os.OpenFile(DownloadLocation+"/"+tempFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
panic(err)
}
defer file.Close()
for i := 0; i < count; i++ {
partFilename := DownloadLocation + "/temp/" + filename + "_" + strconv.Itoa(i)
reader, err := ioutil.ReadFile(partFilename)
reader = reader[16:]
if err != nil {
log.Fatal(err)
panic(err)
}
if _, err = file.WriteString(string(reader)); err != nil {
log.Fatal(err)
panic(err)
}
}
file.Close()
......@@ -102,7 +102,7 @@ func clearFiles(filename string, count int) {
}
empty, err := isDirEmpty(DownloadLocation + "/temp/")
if err != nil {
log.Fatal(err)
panic(err)
}
if empty {
os.Remove(DownloadLocation + "/temp/")
......@@ -112,20 +112,21 @@ func clearFiles(filename string, count int) {
func noOfExistingConnection(filename string, length int) int {
existingFilename := DownloadLocation + "/temp/" + filename + "_0"
if _, err := os.Stat(existingFilename); err != nil {
log.Fatal("No file to resume downloading")
return 0
// panic("No file to resume downloading 1")
}
if _, err := os.Stat(existingFilename); err == nil {
reader, err := ioutil.ReadFile(existingFilename)
if err != nil {
log.Fatal(err)
panic(err)
}
if len(reader) < 16 {
log.Fatal("No file to resume downloading")
panic("No file to resume downloading 2")
}
header := reader[:16]
interval := int(binary.LittleEndian.Uint64(header[8:16])) - int(binary.LittleEndian.Uint64(header[0:8]))
if interval == 0 {
log.Fatal("No file to resume downloading")
panic("No file to resume downloading 3")
}
return (length / interval)
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment