-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
432 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,4 +50,5 @@ dist/ | |
*.state | ||
|
||
# sundry junk used for testing and other fuckery | ||
/tmp | ||
/tmp | ||
*.dot |
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,32 @@ | ||
package dump | ||
|
||
import ( | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/rusq/slackdump/v2/types" | ||
) | ||
|
||
// namer is a helper type to generate filenames for conversations. | ||
type namer struct { | ||
t *template.Template | ||
ext string | ||
} | ||
|
||
// newNamer returns a new namer. It must be called with a valid template. | ||
func newNamer(tmpl string, ext string) (namer, error) { | ||
t, err := template.New("name").Parse(tmpl) | ||
if err != nil { | ||
return namer{}, err | ||
} | ||
return namer{t: t, ext: ext}, nil | ||
} | ||
|
||
// Filename returns the filename for the given conversation. | ||
func (n namer) Filename(conv *types.Conversation) string { | ||
var buf strings.Builder | ||
if err := n.t.Execute(&buf, conv); err != nil { | ||
panic(err) | ||
} | ||
return buf.String() + "." + n.ext | ||
} |
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,68 @@ | ||
package state | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// OpenChunks attempts to open the chunk file linked in the State. If the | ||
// chunk is compressed, it will be decompressed and a temporary file will be | ||
// created. The temporary file will be removed when the OpenChunks is | ||
// closed. | ||
func (st *State) OpenChunks(basePath string) (io.ReadSeekCloser, error) { | ||
f, err := os.Open(filepath.Join(basePath, st.Filename)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if st.IsCompressed { | ||
tf, err := uncompress(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return removeOnClose(tf.Name(), tf), nil | ||
} | ||
return f, nil | ||
} | ||
|
||
func removeOnClose(name string, r io.ReadSeekCloser) io.ReadSeekCloser { | ||
return removeWrapper{filename: name, ReadSeekCloser: r} | ||
} | ||
|
||
type removeWrapper struct { | ||
io.ReadSeekCloser | ||
|
||
filename string | ||
} | ||
|
||
func (r removeWrapper) Close() error { | ||
err := r.ReadSeekCloser.Close() | ||
if err != nil { | ||
return err | ||
} | ||
return os.Remove(r.filename) | ||
} | ||
|
||
// uncompress decompresses a gzip file and returns a temporary file handler. | ||
// it must be removed after use. | ||
func uncompress(r io.Reader) (*os.File, error) { | ||
gr, err := gzip.NewReader(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer gr.Close() | ||
f, err := os.CreateTemp("", "fsadapter-*") | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = io.Copy(f, gr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// reset temporary file position to prepare it for reading. | ||
if _, err := f.Seek(0, io.SeekStart); err != nil { | ||
return nil, err | ||
} | ||
return f, nil | ||
} |
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,54 @@ | ||
package osext | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/rusq/fsadapter" | ||
) | ||
|
||
// MoveFile moves a file from src to dst. If dst already exists, it will be | ||
// overwritten. | ||
// | ||
// Adopted solution from https://stackoverflow.com/questions/50740902/move-a-file-to-a-different-drive-with-go | ||
// TODO: This is a temporary solution. We should use os.Rename() instead, but | ||
// that doesn't work across filesystems, see the above link. | ||
func MoveFile(src string, fs fsadapter.FS, dst string) error { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return fmt.Errorf("unable to open source file: %s", err) | ||
} | ||
|
||
out, err := fs.Create(dst) | ||
if err != nil { | ||
in.Close() | ||
return fmt.Errorf("unable to open destination file: %s", err) | ||
} | ||
defer out.Close() | ||
|
||
_, err = io.Copy(out, in) | ||
in.Close() | ||
if err != nil { | ||
return fmt.Errorf("error writing output: %s", err) | ||
} | ||
|
||
// sync is not supported by fsadapter. | ||
// if err := out.Sync(); err != nil { | ||
// return fmt.Errorf("sync: %s", err) | ||
// } | ||
|
||
if _, err := os.Stat(src); err != nil { | ||
return fmt.Errorf("stat: %s", err) | ||
} else { | ||
// Chmod not yet supported. | ||
// if err := fs.Chmod(dst, si.Mode()); err != nil { | ||
// return fmt.Errorf("chmod: %s", err) | ||
// } | ||
} | ||
|
||
if err := os.Remove(src); err != nil { | ||
return fmt.Errorf("failed removing source: %s", err) | ||
} | ||
return nil | ||
} |
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,2 @@ | ||
// Package osext provides some extended functionality for the os package. | ||
package osext |
Oops, something went wrong.