mirror of
https://github.com/pikami/rss-dl.git
synced 2025-12-23 02:50:02 +00:00
Initial commit
This commit is contained in:
31
fileio/downloader.go
Normal file
31
fileio/downloader.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package fileio
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// DownloadFile - Download file and store it
|
||||
func DownloadFile(outputFilename string, url string) {
|
||||
// Get the data
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(outputFilename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// Write the body to file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
47
fileio/file_rw.go
Normal file
47
fileio/file_rw.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package fileio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// WriteToFile - Writes a string to file
|
||||
func WriteToFile(filename string, content string) {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
l, err := f.WriteString(content)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
f.Close()
|
||||
return
|
||||
}
|
||||
fmt.Println(l, "bytes written successfully")
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// InitOutputDirectory - Creates output directory
|
||||
func InitOutputDirectory(path string) {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
os.Mkdir(path, os.ModePerm)
|
||||
}
|
||||
}
|
||||
|
||||
// CheckIfExists - Checks if file or directory exists
|
||||
func CheckIfExists(path string) bool {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user