Skip to content

Commit

Permalink
Merge pull request #8374 from snipe/fixes/moar_flysystem
Browse files Browse the repository at this point in the history
WIP - More Flysystem fixes
  • Loading branch information
snipe authored Aug 31, 2020
2 parents f83acf8 + f2c60d0 commit 8ec99ff
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 135 deletions.
9 changes: 5 additions & 4 deletions app/Console/Commands/MoveUploadsToNewDisk.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MoveUploadsToNewDisk extends Command
*
* @var string
*/
protected $description = 'This will move your uploaded files to whatever your current disk is.';
protected $description = 'This will move your locally uploaded files to whatever your current disk is.';

/**
* Create a new command instance.
Expand All @@ -42,7 +42,7 @@ public function handle()

if (config('filesystems.default')=='local') {
$this->error('Your current disk is set to local so we cannot proceed.');
$this->warn("Please configure your .env settings for S3. \nChange your PUBLIC_FILESYSTEM_DISK value to 's3_public' and your PRIVATE_FILESYSTEM_DISK to s3_public.");
$this->warn("Please configure your .env settings for S3. \nChange your PUBLIC_FILESYSTEM_DISK value to 's3_public' and your PRIVATE_FILESYSTEM_DISK to s3_private.");
return false;
}
$delete_local = $this->argument('delete_local');
Expand Down Expand Up @@ -84,9 +84,9 @@ public function handle()

}

$logos = glob('public/uploads'."/logo*.*");
$logos = glob('public/uploads'."/setting*.*");
$this->info("\nThere are ".count($logos).' files that might be logos.');
$type_count=0;
$type_count = 0;

for ($l = 0; $l < count($logos); $l++) {
$type_count++;
Expand All @@ -102,6 +102,7 @@ public function handle()
$private_uploads['imports'] = glob('storage/private_uploads/imports'."/*.*");
$private_uploads['licenses'] = glob('storage/private_uploads/licenses'."/*.*");
$private_uploads['users'] = glob('storage/private_uploads/users'."/*.*");
$private_uploads['backups'] = glob('storage/private_uploads/users'."/*.*");


foreach($private_uploads as $private_type => $private_upload)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function store(ImageUploadRequest $request)
$model->fieldset_id = e($request->input('custom_fieldset'));
}

$model = $request->handleImages($model,600,null,null, public_path().'/uploads/models');
$model = $request->handleImages($model);

// Was it created?
if ($model->save()) {
Expand Down
104 changes: 28 additions & 76 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,94 +409,43 @@ public function postBranding(ImageUploadRequest $request)
$setting->custom_css = $request->input('custom_css');
}

$setting = $request->handleImages($setting,600,'logo','', 'logo');

if ($request->hasFile('logo')) {
$image = $request->file('logo');
$ext = $image->getClientOriginalExtension();
$setting->logo = $file_name = 'logo-'.date('U').'.'. $ext;

if ('svg' != $image->getClientOriginalExtension()) {
$upload = Image::make($image->getRealPath())->resize(null, 150, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}

// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($file_name, (string) $upload->encode());

// Remove Current image if exists
if (($setting->logo) && (file_exists($file_name))) {
Storage::disk('public')->delete($file_name);
}

} elseif ('1' == $request->input('clear_logo')) {
if ('1' == $request->input('clear_logo')) {
Storage::disk('public')->delete($setting->logo);
$setting->logo = null;
$setting->brand = 1;
}


if ($request->hasFile('email_logo')) {
$email_image = $email_upload = $request->file('email_logo');
$email_ext = $email_image->getClientOriginalExtension();
$setting->email_logo = $email_file_name = 'email_logo.' . $email_ext;

if ('svg' != $email_image->getClientOriginalExtension()) {
$email_upload = Image::make($email_image->getRealPath())->resize(null, 100, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
$setting = $request->handleImages($setting,600,'email_logo','', 'email_logo');

// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($email_file_name, (string) $email_upload->encode());

// Remove Current image if exists
if (($setting->email_logo) && (file_exists($email_file_name))) {
Storage::disk('public')->delete($email_file_name);
}
} elseif ('1' == $request->input('clear_email_logo')) {
if ('1' == $request->input('clear_email_logo')) {
Storage::disk('public')->delete($setting->email_logo);
$setting->email_logo = null;
// If they are uploading an image, validate it and upload it
}


// If the user wants to clear the label logo...
if ($request->hasFile('label_logo')) {
$image = $request->file('label_logo');
$ext = $image->getClientOriginalExtension();
$setting->label_logo = $label_file_name = 'label_logo.' . $ext;

if ('svg' != $image->getClientOriginalExtension()) {
$upload = Image::make($image->getRealPath())->resize(null, 100, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
$setting = $request->handleImages($setting,600,'label_logo','', 'label_logo');

// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($label_file_name, (string) $upload->encode());

// Remove Current image if exists
if (($setting->label_logo) && (file_exists($label_file_name))) {
Storage::disk('public')->delete($label_file_name);
}
} elseif ('1' == $request->input('clear_label_logo')) {
if ('1' == $request->input('clear_label_logo')) {
Storage::disk('public')->delete($setting->label_logo);
$setting->label_logo = null;

// If they are uploading an image, validate it and upload it
}


// If the user wants to clear the favicon...
if ($request->hasFile('favicon')) {
$favicon_image = $favicon_upload = $request->file('favicon');
$favicon_ext = $favicon_image->getClientOriginalExtension();
$setting->favicon = $favicon_file_name = 'favicon-uploaded.' . $favicon_ext;

if (('ico' != $favicon_image->getClientOriginalExtension()) && ('svg' != $favicon_image->getClientOriginalExtension())) {
if (($favicon_image->getClientOriginalExtension()!='ico') && ($favicon_image->getClientOriginalExtension()!='svg')) {
$favicon_upload = Image::make($favicon_image->getRealPath())->resize(null, 36, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
Expand All @@ -509,11 +458,6 @@ public function postBranding(ImageUploadRequest $request)
}







// Remove Current image if exists
if (($setting->favicon) && (file_exists($favicon_file_name))) {
Storage::disk('public')->delete($favicon_file_name);
Expand Down Expand Up @@ -1064,19 +1008,26 @@ public function postSamlSettings(SettingsSamlRequest $request)
*/
public function getBackups()
{
$path = storage_path() . '/app/' . config('backup.backup.name');

$path = 'backups';
$path = 'app/backups';
$backup_files = Storage::files($path);
$files = [];

if (count($backup_files) > 0) {
for ($f = 0; $f < count($backup_files); ++$f) {
$files[] = [
'filename' => basename($backup_files[$f]),
'filesize' => Setting::fileSizeConvert(Storage::size($backup_files[$f])),
'modified' => Storage::lastModified($backup_files[$f]),
];

// Skip dotfiles like .gitignore and .DS_STORE
if ((substr(basename($backup_files[$f]), 0, 1) != '.')) {

$files[] = [
'filename' => basename($backup_files[$f]),
'filesize' => Setting::fileSizeConvert(Storage::size($backup_files[$f])),
'modified' => Storage::lastModified($backup_files[$f]),
];

}


}
}

Expand Down Expand Up @@ -1124,13 +1075,15 @@ public function postBackups()
*
* @since [v1.8]
*
* @return Redirect
* @return Storage
*/
public function downloadFile($filename = null)
{
$path = 'app/backups';

if (! config('app.lock_passwords')) {
if (Storage::exists($filename)) {
return Response::download(Storage::url('') . e($filename));
if (Storage::exists($path . '/' . $filename)) {
return Storage::download($path . '/' . $filename);
} else {
// Redirect to the backup page
return redirect()->route('settings.backups.index')->with('error', trans('admin/settings/message.backup.file_not_found'));
Expand All @@ -1153,12 +1106,11 @@ public function downloadFile($filename = null)
public function deleteFile($filename = null)
{
if (! config('app.lock_passwords')) {
$path = 'backups';
$path = 'app/backups';

if (Storage::exists($path . '/' . $filename)) {
try {
Storage::delete($path . '/' . $filename);

return redirect()->route('settings.backups.index')->with('success', trans('admin/settings/message.backup.file_deleted'));
} catch (\Exception $e) {
\Log::debug($e);
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Users/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ public function update(SaveUserRequest $request, $id = null)

$user->permissions = json_encode($permissions_array);

app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar');
// Handle uploaded avatar
app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'avatar', 'avatars', 'avatar');


//\Log::debug(print_r($user, true));

// Was the user updated?
if ($user->save()) {
Expand Down
82 changes: 37 additions & 45 deletions app/Http/Requests/ImageUploadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,53 +43,53 @@ public function response(array $errors)
* @param String $path location for uploaded images, defaults to uploads/plural of item type.
* @return SnipeModel Target asset is being checked out to.
*/
public function handleImages($item, $w = 600, $fieldname = 'image', $path = null, $db_fieldname = 'image')
public function handleImages($item, $w = 600, $form_fieldname = null, $path = null, $db_fieldname = 'image')
{
\Log::debug('Handle file upload');


$type = strtolower(class_basename(get_class($item)));

if (is_null($path)) {
$path = str_plural($type);
$path = str_plural($type);

if ($type=='assetmodel') {
\Log::debug('This is an asset model! Override the path');
if ($type == 'assetmodel') {
$path = 'models';
}

if ($type=='user') {
\Log::debug('This is a user! Override the path');
if ($type == 'user') {
$path = 'avatars';
}
}

\Log::info('Path is: '.$path);
if (is_null($form_fieldname)) {
$form_fieldname = 'image';
}

// This is dumb, but we need it for overriding field names for exceptions like avatars and logo uploads
if (is_null($db_fieldname)) {
$use_db_field = $form_fieldname;
} else {
$use_db_field = $db_fieldname;
}


\Log::info('Image path is: '.$path);
\Log::debug('Type is: '.$type);
\Log::debug('Image path is: '.$path);
\Log::debug('Image fieldname is: '.$fieldname);
\Log::debug('Form fieldname is: '.$form_fieldname);
\Log::debug('DB fieldname is: '.$use_db_field);
\Log::debug('Trying to upload to '. $path);

if ($this->hasFile($fieldname)) {
\Log::debug($this->file());

if (!config('app.lock_passwords')) {
if ($this->hasFile($form_fieldname)) {

if (!Storage::disk('public')->exists($path))
{
\Log::debug($path);
// Storage::disk('public')->makeDirectory($path, 775);
}

if (!is_dir($path)) {
\Log::info($path.' does not exist');
//mkdir($path);
}
if (!config('app.lock_passwords')) {

$image = $this->file($fieldname);
$image = $this->file($form_fieldname);
$ext = $image->getClientOriginalExtension();
$file_name = $type.'-'.str_random(18).'.'.$ext;
$file_name = $type.'-'.$form_fieldname.'-'.str_random(10).'.'.$ext;

\Log::info('File name will be: '.$file_name);
\Log::debug('File extension is: '. $ext);

if ($image->getClientOriginalExtension()!=='svg') {
\Log::debug('Not an SVG - resize');
Expand All @@ -102,6 +102,7 @@ public function handleImages($item, $w = 600, $fieldname = 'image', $path = null
// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($path.'/'.$file_name, (string)$upload->encode());


// If the file is an SVG, we need to clean it and NOT encode it
} else {
\Log::debug('This is an SVG');
Expand All @@ -120,40 +121,32 @@ public function handleImages($item, $w = 600, $fieldname = 'image', $path = null


// Remove Current image if exists
if (($item->{$fieldname}) && (Storage::disk('public')->exists($path.'/'.$item->{$fieldname}))) {
\Log::debug('A file already exists that we are replacing - we should delete the old one.');
if (Storage::disk('public')->exists($path.'/'.$item->{$use_db_field})) {

// Assign the new filename as the fieldname
if (is_null($db_fieldname)) {
$item->{$fieldname} = $file_name;
} else {
$item->{$db_fieldname} = $file_name;
}
\Log::debug('A file already exists that we are replacing - we should delete the old one.');
try {
Storage::disk('public')->delete($path.'/'.$item->{$fieldname});
Storage::disk('public')->delete($path.'/'.$item->{$use_db_field});
\Log::debug('Old file '.$path.'/'.$file_name.' has been deleted.');
} catch (\Exception $e) {
\Log::debug('Could not delete old file. '.$path.'/'.$item->{$fieldname}.' does not exist?');
\Log::debug('Could not delete old file. '.$path.'/'.$file_name.' does not exist?');

}
}

$item->{$use_db_field} = $file_name;

}

// If the user isn't uploading anything new but wants to delete their old image, do so
} else {

\Log::debug('No file passed for '.$form_fieldname);
if ($this->input('image_delete')=='1') {

\Log::debug('Deleting image');
try {


if (is_null($db_fieldname)) {
$item->{$fieldname} = null;
Storage::disk('public')->delete($path . '/' . $item->{$fieldname});
} else {
$item->{$db_fieldname} = null;
Storage::disk('public')->delete($path . '/' . $item->{$fieldname});
}
Storage::disk('public')->delete($path . '/' . $item->{$use_db_field});
$item->{$use_db_field} = null;

} catch (\Exception $e) {
\Log::debug($e);
Expand All @@ -163,7 +156,6 @@ public function handleImages($item, $w = 600, $fieldname = 'image', $path = null
}



return $item;
}
}
Loading

0 comments on commit 8ec99ff

Please sign in to comment.