Exemple #1
0
func main() {
	flag.Parse()
	file := flag.Arg(0)

	if !*reconstruct {
		chunker.Chunk(file)
	} else {
		chunker.Reconstruct(file)
	}
}
func getOnlineFile(fileUrl string, fileName string) string {
	finalPath := filepath.Join(getWorkingDirectoryAbsolutePath(), fileName)

	// HEAD request to see if file exist on server
	resp, err := http.Head(fileUrl)
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()

	// If whole file does not exist
	if resp.StatusCode == 404 {
		resp, err = http.Head(fileUrl + `.part0`)
		if err != nil {
			log.Fatalln(err)
		}
		defer resp.Body.Close()

		// If small parts do not exist
		if resp.StatusCode == 404 {
			log.Fatalln(`File`, fileUrl, `not found`)

			// Else, it must be big file
		} else {
			// Download parts
			for i := 0; resp.StatusCode != 404; i++ {
				pathToWrite := filepath.Join(getWorkingDirectoryAbsolutePath(), fileName+`.part`+strconv.Itoa(i))
				downloadIfBadFile(fileUrl+`.part`+strconv.Itoa(i), pathToWrite, fileName+`.part`+strconv.Itoa(i))

				resp, err = http.Head(fileUrl + `.part` + strconv.Itoa(i+1))
				if err != nil {
					log.Fatalln(`Error getting part`, strconv.Itoa(i)+`:`, err)
				}
				defer resp.Body.Close()
			}

			// Reconstruct
			chunker.Reconstruct(finalPath)
		}

		// Else, download like normal
	} else {
		pathToWrite := finalPath
		downloadIfBadFile(fileUrl, pathToWrite, fileName)
	}

	return finalPath
}
func getOfflineFile(path string) string {
	var fileExists bool
	var err error
	if fileExists, err = util.CheckFileExists(path); err != nil {
		log.Fatalln(err)
	}

	if !fileExists {
		// Check if this is a big file and there exists its smaller parts
		var partFileExists bool
		if partFileExists, err = util.CheckFileExists(path + `.part0`); err != nil {
			log.Fatalln(err)
		}

		if !partFileExists {
			log.Fatalln(`File`, path, `does not exist`)
		}

		chunker.Reconstruct(path)
	}

	return path
}