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

Ability to rename files on save #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ Class Photo extends Eloquent implements StaplerableInterface
'keep_old_files' => true
]);

// Define an attachment named 'bar', rename filename to 'foo', with both thumbnail (100x100) and large (300x300) styles,
// using custom url and default_url configurations, with the keep_old_files flag set to true
// (so that older file uploads aren't deleted from the file system) and image cropping turned on:
$this->hasAttachedFile('bar', [
'new_filename' => 'foo',
'styles' => [
'thumbnail' => '100x100#',
'large' => '300x300#'
],
'url' => '/system/:attachment/:id_partition/:style/:filename',
'keep_old_files' => true
]);

// Define an attachment named 'baz' that has a watermarked style. Here, we define a style named 'watermarked'
// that's a closure (so that we can do some complex watermarking stuff):
$this->hasAttachedFile('baz', [
Expand Down
10 changes: 9 additions & 1 deletion src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ public function setUploadedFile($uploadedFile)
}

$this->uploadedFile = FileFactory::create($uploadedFile);
$this->instanceWrite('file_name', $this->uploadedFile->getFilename());

// Rename file if option new_filename exists
if ($this->config->new_filename) {
$ext = pathinfo($this->uploadedFile->getFilename(), PATHINFO_EXTENSION);
$this->instanceWrite('file_name', $this->config->new_filename . '.' . $ext);
} else {
$this->instanceWrite('file_name', $this->uploadedFile->getFilename());
}

$this->instanceWrite('file_size', $this->uploadedFile->getSize());
$this->instanceWrite('content_type', $this->uploadedFile->getMimeType());
$this->instanceWrite('updated_at', date('Y-m-d H:i:s'));
Expand Down