Skip to content

Commit

Permalink
db filter generate file pdf excel
Browse files Browse the repository at this point in the history
  • Loading branch information
DrOctavius committed Apr 7, 2022
1 parent cf83289 commit a670bf9
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 174 deletions.
5 changes: 3 additions & 2 deletions core/clients/db/filter/export_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/google/uuid"
"github.com/kyaxcorp/go-core/core/helpers/conv"
"github.com/kyaxcorp/go-core/core/helpers/file"
"github.com/kyaxcorp/go-core/core/helpers/filesystem"
"github.com/kyaxcorp/go-core/core/helpers/filesystem/tmp"
"github.com/xuri/excelize/v2"
"time"
)
Expand All @@ -15,7 +15,7 @@ func (e *Export) GeneratePdf() {

func (e *Export) GetExportPath(paths ...string) (string, error) {
_paths := append([]string{"exporter"}, paths...)
tmpPath, _err := filesystem.GetAppTmpPath(_paths...)
tmpPath, _err := tmp.GetAppTmpPath(_paths...)
if _err != nil {
return "", _err
}
Expand Down Expand Up @@ -138,6 +138,7 @@ func (e *Export) GenerateExcel() bool {

if e.SelfDeleteAfterSeconds != 0 {
time.AfterFunc(time.Second*time.Duration(e.SelfDeleteAfterSeconds), func() {
// Check if exists...!?
file.Delete(fullFilePath)
})
}
Expand Down
19 changes: 19 additions & 0 deletions core/helpers/file/contents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package file

import (
"io/ioutil"
"os"
)

func PutContentsMode(filename string, data string, mode os.FileMode) error {
return ioutil.WriteFile(filename, []byte(data), mode)
}

func PutContents(filename string, data string) error {
return ioutil.WriteFile(filename, []byte(data), 0751)
}

func GetContents(filename string) (string, error) {
data, err := ioutil.ReadFile(filename)
return string(data), err
}
37 changes: 37 additions & 0 deletions core/helpers/file/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package file

import (
"fmt"
"io"
"os"
)

func Copy(src, dst string) (int64, error) {
src = FilterPath(src)
dst = FilterPath(dst)

//log.Println(src)
//log.Println(dst)
sourceFileStat, _err := os.Stat(src)
if _err != nil {
return 0, _err
}

if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}

source, _err := os.Open(src)
if _err != nil {
return 0, _err
}
defer source.Close()

destination, _err := os.Create(dst)
if _err != nil {
return 0, _err
}
defer destination.Close()
nBytes, _err := io.Copy(destination, source)
return nBytes, _err
}
8 changes: 7 additions & 1 deletion core/helpers/file/delete.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package file

import "os"
import (
"os"
)

func Remove(filePath string) (bool, error) {
return Delete(filePath)
}

func Unlink(filePath string) (bool, error) {
return Delete(filePath)
}

func Delete(filePath string) (bool, error) {
_err := os.Remove(filePath)
if _err != nil {
Expand Down
19 changes: 19 additions & 0 deletions core/helpers/file/delete_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package file

import "os"

func RemoveAll(path string) error {
return os.RemoveAll(path)
}

func RemoveRecursive(path string) error {
return RemoveAll(path)
}

func DeleteAll(path string) error {
return RemoveAll(path)
}

func DeleteRecursive(path string) error {
return RemoveAll(path)
}
11 changes: 11 additions & 0 deletions core/helpers/file/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package file

import "os"

func Exists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
} else {
return false
}
}
39 changes: 39 additions & 0 deletions core/helpers/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package file

import (
"fmt"
"os"
)

func getPathType(path string) uint8 {
fi, err := os.Stat(path)
if err != nil {
fmt.Println(err)
return 0
}
switch mode := fi.Mode(); {
case mode.IsDir():
// do directory stuff
//fmt.Println("directory")
return 1
case mode.IsRegular():
// do file stuff
//fmt.Println("file")
return 2
}
return 0
}

func IsFile(path string) bool {
if getPathType(path) == 2 {
return true
}
return false
}

func IsDir(path string) bool {
if getPathType(path) == 1 {
return true
}
return false
}
14 changes: 14 additions & 0 deletions core/helpers/file/filter_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package file

import (
"github.com/kyaxcorp/go-core/core/helpers/filesystem"
"strings"
)

func FilterPath(path string) string {
p := strings.ReplaceAll(path, `\\`, filesystem.DirSeparator())
p = strings.ReplaceAll(p, "//", filesystem.DirSeparator())
p = strings.ReplaceAll(p, "/", filesystem.DirSeparator())
p = strings.ReplaceAll(p, `\`, filesystem.DirSeparator())
return p
}
141 changes: 0 additions & 141 deletions core/helpers/filesystem/file.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package filesystem
package tmp

import (
"github.com/kyaxcorp/go-core/core/config"
"github.com/kyaxcorp/go-core/core/helpers/err/define"
"github.com/kyaxcorp/go-core/core/helpers/filesystem"
fsPath "github.com/kyaxcorp/go-core/core/helpers/filesystem/path"
"strings"
)
Expand All @@ -18,11 +19,11 @@ func GetAppTmpPath(paths ...string) (string, error) {
}

if len(paths) > 0 {
itemPath = itemPath + strings.Join(paths, DirSeparator())
itemPath = itemPath + strings.Join(paths, filesystem.DirSeparator())
}

if !Exists(itemPath) {
if !MkDir(itemPath) {
if !filesystem.Exists(itemPath) {
if !filesystem.MkDir(itemPath) {
return "", define.Err(0, "failed to create path -> ", itemPath)
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/helpers/folder/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package folder

import "os"

func Exists(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}
1 change: 1 addition & 0 deletions core/helpers/folder/folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package folder
17 changes: 17 additions & 0 deletions core/helpers/folder/mkdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package folder

import "os"

func MkDir(path string) bool {
/*err := os.Mkdir(path, 0751)
if err != nil {
log.Fatal(err)
}*/

err := os.MkdirAll(path, 0751)
if err != nil {
//log.Fatal(err)
return false
}
return true
}
Loading

0 comments on commit a670bf9

Please sign in to comment.