Skip to content

Commit a60d81b

Browse files
authored
Remove suffix from extraction folders. (#11)
* rename temp folder * add rar test
1 parent aa9f920 commit a60d81b

File tree

2 files changed

+90
-22
lines changed

2 files changed

+90
-22
lines changed

queue.go

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ func (x *Xtractr) decompressFolders(resp *Response) error {
153153
}
154154

155155
func (x *Xtractr) finishExtract(resp *Response, err error) {
156+
if resp.X.TempFolder {
157+
x.cleanTempFolder(resp)
158+
}
159+
156160
resp.Error = err
157161
resp.Elapsed = time.Since(resp.Started)
158162
resp.Done = true
@@ -265,40 +269,73 @@ func (x *Xtractr) processArchive(filename, tmpPath string, passwords ...string)
265269

266270
func (x *Xtractr) cleanupProcessedArchives(resp *Response) error {
267271
if resp.X.LogFile {
268-
tmpFile := filepath.Join(resp.Output, x.config.Suffix+"."+filepath.Base(resp.X.SearchPath)+".txt")
269-
resp.NewFiles = append(resp.NewFiles, tmpFile)
270-
271-
msg := []byte(fmt.Sprintf("# %s - this file may be removed with the extracted data\n---\n"+
272-
"archives:%s\nextras:%v\nfrom_path:%s\ntemp_path:%s\nrelocated:%v\ntime:%v\nfiles:\n - %v\n",
273-
x.config.Suffix, resp.Archives, resp.Extras, resp.X.SearchPath, resp.Output, !resp.X.TempFolder, time.Now(),
274-
strings.Join(resp.NewFiles, "\n - ")))
275-
276-
if err := ioutil.WriteFile(tmpFile, msg, x.config.FileMode); err != nil {
277-
x.config.Printf("Error: Creating Temporary Tracking File: %v", err) // continue anyway.
278-
}
272+
x.createLogFile(resp)
279273
}
280274

281275
if resp.X.DeleteOrig {
282-
for _, archives := range resp.Archives {
283-
x.DeleteFiles(archives...) // as requested
284-
}
285-
286-
for _, archives := range resp.Extras {
287-
if len(archives) != 0 {
288-
x.DeleteFiles(archives...) // these got extracted too
289-
}
290-
}
276+
// as requested
277+
x.deleteOriginals(resp)
291278
}
292279

293280
var err error
294281

295-
// If TempFolder is false then move the files back to the original location.
296282
if !resp.X.TempFolder {
283+
// If TempFolder is false then move the files back to the original location.
297284
resp.NewFiles, err = x.MoveFiles(resp.Output, resp.X.SearchPath, false)
298-
} else if len(x.GetFileList(resp.X.SearchPath)) == 0 {
285+
}
286+
287+
if len(x.GetFileList(resp.X.SearchPath)) == 0 {
299288
// If the original path is empty, delete it.
300289
x.DeleteFiles(resp.X.SearchPath)
301290
}
302291

303292
return err
304293
}
294+
295+
func (x *Xtractr) createLogFile(resp *Response) {
296+
tmpFile := filepath.Join(resp.Output, x.config.Suffix+"."+filepath.Base(resp.X.SearchPath)+".txt")
297+
resp.NewFiles = append(resp.NewFiles, tmpFile)
298+
299+
msg := []byte(fmt.Sprintf("# %s - this file may be removed with the extracted data\n---\n"+
300+
"archives:%s\nextras:%v\nfrom_path:%s\ntemp_path:%s\nrelocated:%v\ntime:%v\nfiles:\n - %v\n",
301+
x.config.Suffix, resp.Archives, resp.Extras, resp.X.SearchPath, resp.Output, !resp.X.TempFolder, time.Now(),
302+
strings.Join(resp.NewFiles, "\n - ")))
303+
304+
if err := ioutil.WriteFile(tmpFile, msg, x.config.FileMode); err != nil {
305+
x.config.Printf("Error: Creating Temporary Tracking File: %v", err)
306+
}
307+
}
308+
309+
func (x *Xtractr) deleteOriginals(resp *Response) {
310+
for _, archives := range resp.Archives {
311+
x.DeleteFiles(archives...)
312+
}
313+
// these got extracted too
314+
for _, archives := range resp.Extras {
315+
if len(archives) != 0 {
316+
x.DeleteFiles(archives...)
317+
}
318+
}
319+
}
320+
321+
func (x *Xtractr) cleanTempFolder(resp *Response) {
322+
noSuffix := strings.TrimSuffix(strings.TrimRight(resp.Output, `/\`), x.config.Suffix)
323+
if _, err := os.Stat(noSuffix); err == nil {
324+
return // it exists already?!
325+
} else if _, err := os.Stat(resp.Output); err != nil {
326+
return
327+
}
328+
329+
if newFiles, err := x.MoveFiles(resp.Output, noSuffix, false); err != nil {
330+
x.config.Printf("Error: Renaming Temporary Folder: %v", err)
331+
} else {
332+
x.config.Debugf("Renamed Temp Folder: %v -> %v", resp.Output, noSuffix)
333+
resp.Output = noSuffix
334+
resp.NewFiles = newFiles
335+
}
336+
337+
if len(x.GetFileList(resp.X.SearchPath)) == 0 {
338+
// If the original path is empty, delete it.
339+
x.DeleteFiles(resp.X.SearchPath)
340+
}
341+
}

rar_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package xtractr_test
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"golift.io/xtractr"
10+
)
11+
12+
func TestExtractRAR(t *testing.T) {
13+
t.Parallel()
14+
15+
name, err := ioutil.TempDir(".", "xtractr_test_*_data")
16+
if err != nil {
17+
t.Fatalf("could not make temporary directory: %v", err)
18+
}
19+
defer os.RemoveAll(name) //nolint:wsl
20+
21+
size, files, archives, err := xtractr.ExtractRAR(&xtractr.XFile{
22+
FilePath: "./test_data/archive.rar",
23+
OutputDir: name,
24+
Password: "testing", // one of these is right. :)
25+
Passwords: []string{"testingmore", "some_password", "some_other"},
26+
})
27+
assert.NoError(t, err)
28+
assert.Equal(t, testDataSize, size)
29+
assert.Equal(t, 1, len(archives))
30+
assert.Equal(t, len(filesInTestArchive), len(files))
31+
}

0 commit comments

Comments
 (0)