mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
add file export (#1959)
This commit is contained in:
parent
2bcd6c4771
commit
8a853b1bb7
@ -54,6 +54,11 @@ func (f *File) FileInfo() os.FileInfo {
|
||||
return f.file.FileInfo()
|
||||
}
|
||||
|
||||
// Export exports and saves all its sub files to specified system path `dst` recursively.
|
||||
func (f *File) Export(dst string, option ...ExportOption) error {
|
||||
return f.resource.Export(f.Name(), dst, option...)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (f File) MarshalJSON() ([]byte, error) {
|
||||
info := f.FileInfo()
|
||||
|
@ -237,8 +237,15 @@ func (r *Resource) Export(src, dst string, option ...ExportOption) error {
|
||||
name string
|
||||
path string
|
||||
exportOption ExportOption
|
||||
files = r.doScanDir(src, "*", true, false)
|
||||
files []*File
|
||||
)
|
||||
|
||||
if r.Get(src).FileInfo().IsDir() {
|
||||
files = r.doScanDir(src, "*", true, false)
|
||||
} else {
|
||||
files = append(files, r.Get(src))
|
||||
}
|
||||
|
||||
if len(option) > 0 {
|
||||
exportOption = option[0]
|
||||
}
|
||||
|
@ -109,6 +109,17 @@ func Test_PackWithPrefix2(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Unpack(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
srcPath = gtest.DataPath("testdata.txt")
|
||||
files, err = gres.Unpack(srcPath)
|
||||
)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 63)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Basic(t *testing.T) {
|
||||
gres.Dump()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -269,4 +280,77 @@ func Test_Export(t *testing.T) {
|
||||
nameInSys := `index.html`
|
||||
t.Assert(gfile.GetContents(gfile.Join(dst, nameInSys)), gres.GetContent(nameInRes))
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
src = `template-res/layout1/container.html`
|
||||
dst = gfile.Temp(gtime.TimestampNanoStr())
|
||||
err = gres.Export(src, dst, gres.ExportOption{
|
||||
RemovePrefix: `template-res`,
|
||||
})
|
||||
)
|
||||
defer gfile.Remove(dst)
|
||||
t.AssertNil(err)
|
||||
files, err := gfile.ScanDir(dst, "*", true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IsEmpty(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gres.IsEmpty(), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFile_Name(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
src = `template-res`
|
||||
)
|
||||
t.Assert(gres.Get(src).Name(), src)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFile_Export(t *testing.T) {
|
||||
gres.Dump()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
src = `template-res`
|
||||
dst = gfile.Temp(gtime.TimestampNanoStr())
|
||||
err = gres.Get(src).Export(dst)
|
||||
)
|
||||
defer gfile.Remove(dst)
|
||||
t.AssertNil(err)
|
||||
files, err := gfile.ScanDir(dst, "*", true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 14)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
src = `template-res`
|
||||
dst = gfile.Temp(gtime.TimestampNanoStr())
|
||||
err = gres.Get(src).Export(dst, gres.ExportOption{
|
||||
RemovePrefix: `template-res`,
|
||||
})
|
||||
)
|
||||
defer gfile.Remove(dst)
|
||||
t.AssertNil(err)
|
||||
files, err := gfile.ScanDir(dst, "*", true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 13)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
src = `template-res/layout1/container.html`
|
||||
dst = gfile.Temp(gtime.TimestampNanoStr())
|
||||
err = gres.Get(src).Export(dst, gres.ExportOption{
|
||||
RemovePrefix: `template-res`,
|
||||
})
|
||||
)
|
||||
defer gfile.Remove(dst)
|
||||
t.AssertNil(err)
|
||||
files, err := gfile.ScanDir(dst, "*", true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(files), 2)
|
||||
})
|
||||
}
|
||||
|
1
os/gres/testdata/testdata.txt
vendored
Normal file
1
os/gres/testdata/testdata.txt
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user