Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added uniqid() filename option #63

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
.idea
19 changes: 19 additions & 0 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Attachment
* @var $filename Filename
*/
public $filename;
/**
* @var $uniqueFilename Unique filename using uniqid()
*/
public $uniqueFilename;
/**
* @var $contentType Mime Type
*/
Expand Down Expand Up @@ -45,13 +49,18 @@ class Attachment

public function __construct(
$filename,
$uniqueFilename,
$contentType,
$stream,
$contentDisposition = 'attachment',
$contentId = '',
$headers = array()
) {
$this->filename = $filename;

// Explode filename to get extension
$this->uniqueFilename = $uniqueFilename . "." . array_pop(explode(".", $filename));

$this->contentType = $contentType;
$this->stream = $stream;
$this->content = null;
Expand All @@ -69,6 +78,16 @@ public function getFilename()
return $this->filename;
}

/**
* retrieve the attachment unique filename
* @return String
*/
public function getUniqueFilename()
{
return $this->uniqueFilename;
}


/**
* Retrieve the Attachment Content-Type
* @return String
Expand Down
7 changes: 5 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public function getAttachments()

$attachments[] = new Attachment(
$filename,
uniqid(),
$this->getPart('content-type', $part),
$this->getAttachmentStream($part),
$disposition,
Expand All @@ -308,7 +309,7 @@ public function getAttachments()
* @return array Saved attachments paths
* @param $attach_dir String of the directory
*/
public function saveAttachments($attach_dir)
public function saveAttachments($attach_dir, $use_uniqueFilename = false)
{
$attachments = $this->getAttachments();
if (empty($attachments)) {
Expand All @@ -321,7 +322,9 @@ public function saveAttachments($attach_dir)

$attachments_paths = array();
foreach ($attachments as $attachment) {
$attachment_path = $attach_dir.$attachment->getFilename();

$attachment_path = $use_uniqueFilename ? $attach_dir . $attachment->getUniqueFilename() : $attach_dir . $attachment->getFilename();

if ($fp = fopen($attachment_path, 'w')) {
while ($bytes = $attachment->read()) {
fwrite($fp, $bytes);
Expand Down
99 changes: 78 additions & 21 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function provideData()
1,
'application/octet-stream',
'attachment',
'04c1d5793efa97c956d011a8b3309f05'
'04c1d5793efa97c956d011a8b3309f05',
'560f9bedcae6c'
)
),
0),
Expand All @@ -72,7 +73,8 @@ public function provideData()
8,
'application/octet-stream',
'attachment',
'18f541cc6bf49209d2bf327ecb887355'
'18f541cc6bf49209d2bf327ecb887355',
'560f9c0471c22'
)
),
0),
Expand All @@ -91,7 +93,8 @@ public function provideData()
48,
'application/octet-stream',
'attachment',
'8734417734fabfa783df6fed0ccf7a4a'
'8734417734fabfa783df6fed0ccf7a4a',
'560f9c171afa3'
)
),
0),
Expand All @@ -110,7 +113,8 @@ public function provideData()
242,
'application/octet-stream',
'attachment',
'c0b5348ef825bf62ba2d07d70d4b9560'
'c0b5348ef825bf62ba2d07d70d4b9560',
'560f9c28265fa'
)
),
0),
Expand All @@ -129,7 +133,8 @@ public function provideData()
484,
'application/octet-stream',
'attachment',
'1ced323befc39ebbc147e7588d11ab08'
'1ced323befc39ebbc147e7588d11ab08',
'560f9c39c00ad'
)
),
0),
Expand All @@ -148,7 +153,8 @@ public function provideData()
968,
'application/octet-stream',
'attachment',
'5dc6470ab63e86e8f68d88afb11556fe'
'5dc6470ab63e86e8f68d88afb11556fe',
'560f9c49ceccb'
)
),
0),
Expand All @@ -167,7 +173,8 @@ public function provideData()
4,
'application/octet-stream',
'attachment',
'0e6d510323b009da939070faf72e521c'
'0e6d510323b009da939070faf72e521c',
'560f9c595bd14'
)
),
0),
Expand All @@ -186,7 +193,8 @@ public function provideData()
0,
'image/gif',
'inline',
'102aa12e16635bf2b0b39ef6a91aa95c'
'102aa12e16635bf2b0b39ef6a91aa95c',
'560f9c7a98585.jpg'
),
array(
'background.jpg',
Expand All @@ -195,7 +203,8 @@ public function provideData()
0,
'image/gif',
'inline',
'798f976a5834019d3f2dd087be5d5796'
'798f976a5834019d3f2dd087be5d5796',
'560f9c8b2be67.jpg'
),
array(
'attachment.txt',
Expand All @@ -204,7 +213,8 @@ public function provideData()
4,
'text/plain',
'attachment',
'71fff85a7960460bdd3c4b8f1ee9279b'
'71fff85a7960460bdd3c4b8f1ee9279b',
'560f9c9f1fdb2.txt'
)
),
2),
Expand Down Expand Up @@ -232,7 +242,8 @@ public function provideData()
726,
'application/octet-stream',
'attachment',
'8da4b0177297b1d7f061e44d64cc766f'
'8da4b0177297b1d7f061e44d64cc766f',
'560f9cbd025ac'
)
),
0),
Expand All @@ -251,7 +262,8 @@ public function provideData()
1,
'text/plain',
'attachment',
'839d0486dd1b91e520d456bb17c33148'
'839d0486dd1b91e520d456bb17c33148',
'560f9cae72fc6'
)
),
0),
Expand All @@ -270,7 +282,8 @@ public function provideData()
1,
'text/plain',
'attachment',
'839d0486dd1b91e520d456bb17c33148'
'839d0486dd1b91e520d456bb17c33148',
'560f9cdb2a354.txt'
)
),
0),
Expand All @@ -289,7 +302,8 @@ public function provideData()
0,
'application/pdf',
'attachment',
'ffe2cb0f5df4e2cfffd3931b6566f3cb'
'ffe2cb0f5df4e2cfffd3931b6566f3cb',
'560f9ceede393.pdf'
)
),
0),
Expand All @@ -308,7 +322,8 @@ public function provideData()
2,
'text/plain',
'inline',
'865238356eec20b67ce8c33c68d8a95a'
'865238356eec20b67ce8c33c68d8a95a',
'560f9cfe7e355.txt'
)
),
0),
Expand All @@ -327,7 +342,8 @@ public function provideData()
1,
'text/plain',
'inline',
'87caaaf9bf1d7ebc2769254710c38a0d'
'87caaaf9bf1d7ebc2769254710c38a0d',
'560f9d109a994'
),
array(
'noname2',
Expand All @@ -336,7 +352,8 @@ public function provideData()
82,
'text/html',
'inline',
'b70ff760112a71009d8295c34fd67d9b'
'b70ff760112a71009d8295c34fd67d9b',
'560f9d1f6bf04'
)
),
0),
Expand All @@ -355,7 +372,8 @@ public function provideData()
2,
'text/plain',
'inline',
'865238356eec20b67ce8c33c68d8a95a'
'865238356eec20b67ce8c33c68d8a95a',
'560fa14f12ab4.txt'
)
),
0),
Expand All @@ -374,7 +392,8 @@ public function provideData()
0,
'image/jpeg',
'attachment',
'567f29989506f21cea8ac992d81ce4c1'
'567f29989506f21cea8ac992d81ce4c1',
'560fa17f7a303.JPG'
),
array(
'ATT00001.txt',
Expand All @@ -383,7 +402,8 @@ public function provideData()
1,
'text/plain',
'attachment',
'095f96b9d5a25d051ad425356745334f'
'095f96b9d5a25d051ad425356745334f',
'560fa18c7d911.txt'
)
),
0),
Expand Down Expand Up @@ -411,7 +431,8 @@ public function provideData()
0,
'text/calendar',
'attachment',
'bf7bfb9b8dd11ff0c830b2388560d434'
'bf7bfb9b8dd11ff0c830b2388560d434',
'560fa19f52c89'
)
),
0),
Expand Down Expand Up @@ -508,13 +529,22 @@ public function testFromPath(
//Save attachments
$Parser->saveAttachments($attach_dir);

//Save attachments with unique filename
$Parser->saveAttachments($attach_dir, true);

foreach ($attachmentsExpected as $attachmentExpected) {
//Test Exist Attachment
$this->assertTrue(file_exists($attach_dir.$attachmentExpected[0]));

//Test Filename Attachment
$this->assertEquals($attachmentExpected[0], $attachments[$iterAttachments]->getFilename());

//Test Exist Attachment (unique filename)
$this->assertTrue(file_exists($attach_dir.$attachmentExpected[7]));

//Test Filename Attachment (unique filename)
$this->assertEquals($attachmentExpected[0], $attachments[$iterAttachments]->getUniqueFilename());

//Test Size Attachment
$this->assertEquals(
$attachmentExpected[1],
Expand Down Expand Up @@ -549,6 +579,9 @@ public function testFromPath(
//Remove Attachment
unlink($attach_dir.$attachments[$iterAttachments]->getFilename());

//Remove Attachment (unique filename)
unlink($attach_dir.$attachments[$iterAttachments]->getUniqueFilename());

$iterAttachments++;

}
Expand Down Expand Up @@ -624,13 +657,22 @@ public function testFromText(
//Save attachments
$Parser->saveAttachments($attach_dir);

//Save attachments with unique filename
$Parser->saveAttachments($attach_dir, true);

foreach ($attachmentsExpected as $attachmentExpected) {
//Test Exist Attachment
$this->assertTrue(file_exists($attach_dir.$attachmentExpected[0]));

//Test Filename Attachment
$this->assertEquals($attachmentExpected[0], $attachments[$iterAttachments]->getFilename());

//Test Exist Attachment (unique filename)
$this->assertTrue(file_exists($attach_dir.$attachmentExpected[7]));

//Test Filename Attachment (unique filename)
$this->assertEquals($attachmentExpected[0], $attachments[$iterAttachments]->getUniqueFilename());

//Test Size Attachment
$this->assertEquals(
$attachmentExpected[1],
Expand Down Expand Up @@ -665,6 +707,9 @@ public function testFromText(
//Remove Attachment
unlink($attach_dir.$attachments[$iterAttachments]->getFilename());

//Remove Attachment (unique filename)
unlink($attach_dir.$attachments[$iterAttachments]->getUniqueFilename());

$iterAttachments++;

}
Expand Down Expand Up @@ -741,13 +786,22 @@ public function testFromStream(
//Save attachments
$Parser->saveAttachments($attach_dir);

//Save attachments with unique filename
$Parser->saveAttachments($attach_dir, true);

foreach ($attachmentsExpected as $attachmentExpected) {
//Test Exist Attachment
$this->assertTrue(file_exists($attach_dir.$attachmentExpected[0]));

//Test Filename Attachment
$this->assertEquals($attachmentExpected[0], $attachments[$iterAttachments]->getFilename());

//Test Exist Attachment (unique filename)
$this->assertTrue(file_exists($attach_dir.$attachmentExpected[7]));

//Test Filename Attachment (unique filename)
$this->assertEquals($attachmentExpected[0], $attachments[$iterAttachments]->getUniqueFilename());

//Test Size Attachment
$this->assertEquals(
$attachmentExpected[1],
Expand Down Expand Up @@ -782,6 +836,9 @@ public function testFromStream(
//Remove Attachment
unlink($attach_dir.$attachments[$iterAttachments]->getFilename());

//Remove Attachment (unique filename)
unlink($attach_dir.$attachments[$iterAttachments]->getUniqueFilename());

$iterAttachments++;

}
Expand Down