-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use machine's local time zone - Provide (encrypted) archive size.
- Loading branch information
Showing
4 changed files
with
64 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package backup | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jixunmoe-go/backups/utils/humanize" | ||
"os" | ||
"path" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
var reFileName = regexp.MustCompile(`^(\d+)\.bin$`) | ||
|
||
type ArchiveInfo struct { | ||
FileName string | ||
Time int64 | ||
Project string | ||
} | ||
|
||
func createArchiveFromName(project, name string) *ArchiveInfo { | ||
m := reFileName.FindStringSubmatch(name) | ||
ts, err := strconv.ParseInt(m[1], 10, 64) | ||
if err != nil { | ||
return nil | ||
} | ||
return &ArchiveInfo{ | ||
FileName: name, | ||
Time: ts, | ||
Project: project, | ||
} | ||
} | ||
|
||
func (a *ArchiveInfo) GetPath() string { | ||
return path.Join(GetBackupLocation(a.Project), a.FileName) | ||
} | ||
|
||
func (a *ArchiveInfo) GetFormattedSize() string { | ||
fi, err := os.Stat(a.GetPath()) | ||
if err != nil { | ||
return fmt.Sprintf("(unknown size: %s)", err) | ||
} | ||
return humanize.ByteCountBinary(fi.Size()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package humanize | ||
|
||
import "fmt" | ||
|
||
// https://programming.guide/go/formatting-byte-size-to-human-readable-format.html | ||
func ByteCountBinary(b int64) string { | ||
const unit = 1024 | ||
if b < unit { | ||
return fmt.Sprintf("%d B", b) | ||
} | ||
div, exp := int64(unit), 0 | ||
for n := b / unit; n >= unit; n /= unit { | ||
div *= unit | ||
exp++ | ||
} | ||
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp]) | ||
} |