Skip to content

Commit

Permalink
Start fixing the combined flags -a and -p
Browse files Browse the repository at this point in the history
  • Loading branch information
tagatac committed Nov 17, 2024
1 parent f767ab8 commit 20c3ea2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
1 change: 1 addition & 0 deletions chatdb/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type Attachment struct {
ID int
Filename string
Filepath string
MIMEType string
TransferName string
}
Expand Down
35 changes: 16 additions & 19 deletions internal/bagoup/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ func (cfg *configuration) handleAttachments(outFile opsys.OutFile, msgID int, at
return nil
}
for _, att := range msgPaths {
attPath, mimeType, transferName := att.Filename, att.MIMEType, att.TransferName
err := cfg.validateAttachmentPath(attPath)
att.Filepath = filepath.Join(cfg.Options.AttachmentsPath, att.Filename)
err := cfg.validateAttachmentPath(att)
if _, ok := err.(errorMissingAttachment); ok {
// Attachment is missing. Just reference it, and skip copying/embedding.
cfg.counts.attachmentsMissing++
log.Printf("WARN: chat file %q - message %d - %s attachment %q (ID %d) - %s", outFile.Name(), msgID, mimeType, transferName, att.ID, err)
if err := outFile.ReferenceAttachment(transferName); err != nil {
return errors.Wrapf(err, "reference attachment %q", transferName)
log.Printf("WARN: chat file %q - message %d - %s attachment %q (ID %d) - %s", outFile.Name(), msgID, att.MIMEType, att.TransferName, att.ID, err)
if err := outFile.ReferenceAttachment(att.TransferName); err != nil {
return errors.Wrapf(err, "reference attachment %q", att.TransferName)
}
cfg.counts.attachments[mimeType]++
cfg.counts.attachments[att.MIMEType]++
continue
} else if err != nil {
return err
Expand All @@ -175,13 +175,12 @@ type errorMissingAttachment struct{ err error }

func (e errorMissingAttachment) Error() string { return e.err.Error() }

func (cfg configuration) validateAttachmentPath(attPath string) error {
if attPath == "" {
func (cfg configuration) validateAttachmentPath(att chatdb.Attachment) error {
if att.Filename == "" {
return errorMissingAttachment{err: errors.New("attachment has no local filename")}
}
attPath = filepath.Join(cfg.Options.AttachmentsPath, attPath)
if ok, err := cfg.OS.FileExist(attPath); err != nil {
return errors.Wrapf(err, "check existence of file %q - POSSIBLE FIX: %s", attPath, _readmeURL)
if ok, err := cfg.OS.FileExist(att.Filepath); err != nil {
return errors.Wrapf(err, "check existence of file %q - POSSIBLE FIX: %s", att.Filepath, _readmeURL)
} else if !ok {
return errorMissingAttachment{err: errors.New("attachment does not exist locally")}
}
Expand All @@ -192,27 +191,25 @@ func (cfg *configuration) copyAttachment(att *chatdb.Attachment, attDir string)
if !cfg.Options.CopyAttachments {
return nil
}
attPath, mimeType := att.Filename, att.MIMEType
unique := true
if cfg.Options.PreservePaths {
unique = false
attDir = filepath.Join(cfg.Options.ExportPath, PreservedPathDir, filepath.Dir(attPath))
attDir = filepath.Join(cfg.Options.ExportPath, PreservedPathDir, filepath.Dir(att.Filename))
if err := cfg.OS.MkdirAll(attDir, os.ModePerm); err != nil {
return errors.Wrapf(err, "create directory %q", attDir)
}
}
attPath = filepath.Join(cfg.Options.AttachmentsPath, attPath)
dstPath, err := cfg.OS.CopyFile(attPath, attDir, unique)
dstPath, err := cfg.OS.CopyFile(att.Filepath, attDir, unique)
if err != nil {
return errors.Wrapf(err, "copy attachment %q to %q", attPath, attDir)
return errors.Wrapf(err, "copy attachment %q to %q", att.Filepath, attDir)
}
att.Filename = dstPath
cfg.counts.attachmentsCopied[mimeType]++
att.Filepath = dstPath
cfg.counts.attachmentsCopied[att.MIMEType]++
return nil
}

func (cfg *configuration) writeAttachment(outFile opsys.OutFile, att chatdb.Attachment) error {
attPath, mimeType := filepath.Join(cfg.Options.AttachmentsPath, att.Filename), att.MIMEType
attPath, mimeType := att.Filepath, att.MIMEType
if cfg.Options.OutputPDF {
if jpgPath, err := cfg.OS.HEIC2JPG(attPath); err != nil {
cfg.counts.conversionsFailed++
Expand Down
66 changes: 50 additions & 16 deletions internal/bagoup/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestWriteFile(t *testing.T) {
ofMock.EXPECT().WriteAttachment("attachment1.jpeg").Return(true, nil),
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil),
osMock.EXPECT().HEIC2JPG("attachment2.jpeg").Return("attachment2.jpeg", nil),
ofMock.EXPECT().WriteAttachment("attachment2.jpeg"),
ofMock.EXPECT().WriteAttachment("attachment2.jpeg").Return(true, nil),
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"),
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"),
ofMock.EXPECT().Stage(),
Expand All @@ -86,7 +86,7 @@ func TestWriteFile(t *testing.T) {
)
},
wantJPGs: 2,
wantEmbedded: 1,
wantEmbedded: 2,
wantConv: 1,
},
{
Expand All @@ -103,10 +103,10 @@ func TestWriteFile(t *testing.T) {
ofMock.EXPECT().WriteMessage("message2"),
osMock.EXPECT().FileExist("attachment1.heic").Return(true, nil),
osMock.EXPECT().HEIC2JPG("attachment1.heic").Return("attachment1.jpeg", nil),
ofMock.EXPECT().WriteAttachment("attachment1.jpeg"),
ofMock.EXPECT().WriteAttachment("attachment1.jpeg").Return(true, nil),
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil),
osMock.EXPECT().HEIC2JPG("attachment2.jpeg").Return("attachment2.jpeg", nil),
ofMock.EXPECT().WriteAttachment("attachment2.jpeg"),
ofMock.EXPECT().WriteAttachment("attachment2.jpeg").Return(true, nil),
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"),
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"),
ofMock.EXPECT().Stage().Return(500, nil),
Expand All @@ -115,8 +115,9 @@ func TestWriteFile(t *testing.T) {
ofMock.EXPECT().Flush(),
)
},
wantJPGs: 2,
wantConv: 1,
wantJPGs: 2,
wantEmbedded: 2,
wantConv: 1,
},
{
msg: "copy attachments",
Expand All @@ -132,11 +133,11 @@ func TestWriteFile(t *testing.T) {
dbMock.EXPECT().GetMessage(2, nil).Return("message2", true, nil),
ofMock.EXPECT().WriteMessage("message2"),
osMock.EXPECT().FileExist("attachment1.heic").Return(true, nil),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/friend/attachments", true).Return("attachment1.heic", nil),
ofMock.EXPECT().WriteAttachment("attachment1.heic"),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/friend/attachments", true).Return("messages-export/friend/attachments/attachment1.heic", nil),
ofMock.EXPECT().WriteAttachment("messages-export/friend/attachments/attachment1.heic"),
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil),
osMock.EXPECT().CopyFile("attachment2.jpeg", "messages-export/friend/attachments", true).Return("attachment2.jpeg", nil),
ofMock.EXPECT().WriteAttachment("attachment2.jpeg"),
osMock.EXPECT().CopyFile("attachment2.jpeg", "messages-export/friend/attachments", true).Return("messages-export/friend/attachments/attachment2.jpeg", nil),
ofMock.EXPECT().WriteAttachment("messages-export/friend/attachments/attachment2.jpeg"),
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"),
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"),
ofMock.EXPECT().Stage(),
Expand All @@ -161,12 +162,12 @@ func TestWriteFile(t *testing.T) {
ofMock.EXPECT().WriteMessage("message2"),
osMock.EXPECT().FileExist("attachment1.heic").Return(true, nil),
osMock.EXPECT().MkdirAll("messages-export/bagoup-attachments", os.ModePerm),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/bagoup-attachments", false).Return("attachment1.heic", nil),
ofMock.EXPECT().WriteAttachment("attachment1.heic"),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/bagoup-attachments", false).Return("messages-export/bagoup-attachments/attachment1.heic", nil),
ofMock.EXPECT().WriteAttachment("messages-export/bagoup-attachments/attachment1.heic"),
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil),
osMock.EXPECT().MkdirAll("messages-export/bagoup-attachments", os.ModePerm),
osMock.EXPECT().CopyFile("attachment2.jpeg", "messages-export/bagoup-attachments", false).Return("attachment2-1.jpeg", nil),
ofMock.EXPECT().WriteAttachment("attachment2-1.jpeg"),
osMock.EXPECT().CopyFile("attachment2.jpeg", "messages-export/bagoup-attachments", false).Return("messages-export/bagoup-attachments/attachment2-1.jpeg", nil),
ofMock.EXPECT().WriteAttachment("messages-export/bagoup-attachments/attachment2-1.jpeg"),
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"),
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"),
ofMock.EXPECT().Stage(),
Expand All @@ -176,6 +177,39 @@ func TestWriteFile(t *testing.T) {
},
wantJPGs: 1,
},
{
msg: "copy attachments and pdf export",
copyAttachments: true,
pdf: true,
setupMocks: func(dbMock *mock_chatdb.MockChatDB, osMock *mock_opsys.MockOS, ofMock *mock_opsys.MockOutFile) {
gomock.InOrder(
osMock.EXPECT().MkdirAll("messages-export/friend", os.ModePerm),
osMock.EXPECT().MkdirAll("messages-export/friend/attachments", os.ModePerm),
osMock.EXPECT().Create("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]").Return(chatFile, nil),
osMock.EXPECT().NewPDFOutFile(chatFile, gomock.Any(), false).Return(ofMock),
dbMock.EXPECT().GetMessage(1, nil).Return("message1", true, nil),
ofMock.EXPECT().WriteMessage("message1"),
dbMock.EXPECT().GetMessage(2, nil).Return("message2", true, nil),
ofMock.EXPECT().WriteMessage("message2"),
osMock.EXPECT().FileExist("attachment1.heic").Return(true, nil),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/friend/attachments", true).Return("messages-export/friend/attachments/attachment1.heic", nil),
osMock.EXPECT().HEIC2JPG("messages-export/friend/attachments/attachment1.heic").Return("attachment1.jpeg", nil),
ofMock.EXPECT().WriteAttachment("attachment1.jpeg").Return(true, nil),
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil),
osMock.EXPECT().CopyFile("attachment2.jpeg", "messages-export/friend/attachments", true).Return("messages-export/friend/attachments/attachment2.jpeg", nil),
osMock.EXPECT().HEIC2JPG("messages-export/friend/attachments/attachment2.jpeg").Return("messages-export/friend/attachments/attachment2.jpeg", nil),
ofMock.EXPECT().WriteAttachment("messages-export/friend/attachments/attachment2.jpeg").Return(true, nil),
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"),
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"),
ofMock.EXPECT().Stage(),
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil),
ofMock.EXPECT().Flush(),
)
},
wantJPGs: 2,
wantEmbedded: 2,
wantConv: 1,
},
{
msg: "chat directory creation error",
pdf: true,
Expand Down Expand Up @@ -451,8 +485,8 @@ func TestWriteFile(t *testing.T) {
dbMock.EXPECT().GetMessage(2, nil).Return("message2", true, nil),
ofMock.EXPECT().WriteMessage("message2"),
osMock.EXPECT().FileExist("attachment1.heic").Return(true, nil),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/friend/attachments", true).Return("attachment1.heic", nil),
ofMock.EXPECT().WriteAttachment("attachment1.heic"),
osMock.EXPECT().CopyFile("attachment1.heic", "messages-export/friend/attachments", true).Return("messages-export/friend/attachments/attachment1.heic", nil),
ofMock.EXPECT().WriteAttachment("messages-export/friend/attachments/attachment1.heic"),
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil),
osMock.EXPECT().CopyFile("attachment2.jpeg", "messages-export/friend/attachments", true).Return("", errors.New("this is a permissions error")),
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"),
Expand Down

0 comments on commit 20c3ea2

Please sign in to comment.