You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
2.2 KiB
119 lines
2.2 KiB
5 years ago
|
package utils
|
||
|
|
||
|
import (
|
||
4 years ago
|
"archive/zip"
|
||
|
"bytes"
|
||
4 years ago
|
"fmt"
|
||
5 years ago
|
"io/ioutil"
|
||
|
"os"
|
||
4 years ago
|
"path"
|
||
|
"strings"
|
||
4 years ago
|
"time"
|
||
4 years ago
|
|
||
3 years ago
|
"hack-browser-data/internal/log"
|
||
5 years ago
|
)
|
||
|
|
||
4 years ago
|
const Prefix = "[x]: "
|
||
|
|
||
4 years ago
|
func IntToBool(a int) bool {
|
||
|
switch a {
|
||
|
case 0, -1:
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
3 years ago
|
func BookmarkType(a int64) string {
|
||
4 years ago
|
switch a {
|
||
|
case 1:
|
||
|
return "url"
|
||
|
default:
|
||
|
return "folder"
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func TimeStampFormat(stamp int64) time.Time {
|
||
|
s1 := time.Unix(stamp, 0)
|
||
4 years ago
|
if s1.Local().Year() > 9999 {
|
||
|
return time.Date(9999, 12, 13, 23, 59, 59, 0, time.Local)
|
||
|
}
|
||
4 years ago
|
return s1
|
||
|
}
|
||
|
|
||
4 years ago
|
func TimeEpochFormat(epoch int64) time.Time {
|
||
4 years ago
|
maxTime := int64(99633311740000000)
|
||
4 years ago
|
if epoch > maxTime {
|
||
4 years ago
|
return time.Date(2049, 1, 1, 1, 1, 1, 1, time.Local)
|
||
4 years ago
|
}
|
||
4 years ago
|
t := time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC)
|
||
|
d := time.Duration(epoch)
|
||
|
for i := 0; i < 1000; i++ {
|
||
|
t = t.Add(d)
|
||
|
}
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func ReadFile(filename string) (string, error) {
|
||
|
s, err := ioutil.ReadFile(filename)
|
||
|
return string(s), err
|
||
|
}
|
||
|
|
||
4 years ago
|
func WriteFile(filename string, data []byte) error {
|
||
|
err := ioutil.WriteFile(filename, data, 0644)
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
func FormatFilename(dir, browser, filename, format string) string {
|
||
4 years ago
|
r := strings.Replace(strings.TrimSpace(strings.ToLower(browser)), " ", "_", -1)
|
||
4 years ago
|
p := path.Join(dir, fmt.Sprintf("%s_%s.%s", r, filename, format))
|
||
4 years ago
|
return p
|
||
|
}
|
||
|
|
||
4 years ago
|
func MakeDir(dirName string) error {
|
||
4 years ago
|
if _, err := os.Stat(dirName); os.IsNotExist(err) {
|
||
4 years ago
|
return os.Mkdir(dirName, 0700)
|
||
4 years ago
|
}
|
||
4 years ago
|
return nil
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
func Compress(exportDir string) error {
|
||
|
files, err := ioutil.ReadDir(exportDir)
|
||
|
if err != nil {
|
||
|
log.Error(err)
|
||
|
}
|
||
|
var b = new(bytes.Buffer)
|
||
|
zw := zip.NewWriter(b)
|
||
|
for _, f := range files {
|
||
|
fw, _ := zw.Create(f.Name())
|
||
|
fileName := path.Join(exportDir, f.Name())
|
||
|
fileContent, err := ioutil.ReadFile(fileName)
|
||
|
if err != nil {
|
||
|
zw.Close()
|
||
|
return err
|
||
|
}
|
||
|
_, err = fw.Write(fileContent)
|
||
|
if err != nil {
|
||
|
zw.Close()
|
||
|
return err
|
||
|
}
|
||
|
err = os.Remove(fileName)
|
||
|
if err != nil {
|
||
|
log.Error(err)
|
||
|
}
|
||
|
}
|
||
|
if err := zw.Close(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
zipName := exportDir + `/archive.zip`
|
||
|
outFile, _ := os.Create(zipName)
|
||
|
_, err = b.WriteTo(outFile)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
fmt.Printf("%s Compress success, zip filename is %s \n", Prefix, zipName)
|
||
|
return nil
|
||
|
}
|