Skip to content

Commit

Permalink
- Using this in production now.
Browse files Browse the repository at this point in the history
- Added a storage saving method by only writing the file to the disk if it
  has a unique MD5.
  • Loading branch information
tylercd100 committed Aug 25, 2016
1 parent 026fc9e commit 86f7f8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-upload` will be documented in this file.

### 1.0.0
- Using this in production now.
- Added a storage saving method by only writing the file to the disk if it has a unique MD5.

### 0.0.2
- Fixed tests.
- Automatically saves model to DB.
Expand Down
32 changes: 22 additions & 10 deletions src/Upload.php
Expand Up @@ -18,20 +18,32 @@ public function __construct()

public function create(UploadedFile $file)
{
$disk = $this->config['disk'];
$filename = $this->getRandomString(32);
$extension = $file->extension();
$directory = $this->getRandomDirectory();
$contents = file_get_contents($file);
$mime = mime_content_type($file->path());
$fingerprint = md5_file($file->path());

$path = "/{$directory}/{$filename}.{$extension}";
$duplicate = File::where('fingerprint', $fingerprint)->first();

if (!$duplicate) {
$disk = $this->config['disk'];
$filename = $this->getRandomString(32);
$extension = $file->extension();
$directory = $this->getRandomDirectory();
$contents = file_get_contents($file);
$mime = mime_content_type($file->path());

$result = $this->getDisk()->put($path, $contents);
$path = "/{$directory}/{$filename}.{$extension}";

if ($result === false) {
throw new Exception("Could not save the file on the disk! Disk: ".$disk);
$result = $this->getDisk()->put($path, $contents);

if ($result === false) {
throw new Exception("Could not save the file on the disk! Disk: ".$disk);
}
} else {
$filename = $duplicate->filename;
$mime = $duplicate->mime;
$path = $duplicate->path;
$disk = $duplicate->disk;
$extension = $duplicate->extension;
$fingerprint = $duplicate->fingerprint;
}

$model = new File(
Expand Down

0 comments on commit 86f7f8c

Please sign in to comment.