PHP code example of reshadman / file-secretary

1. Go to this page and download the library: Download reshadman/file-secretary library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reshadman / file-secretary example snippets



return [
    // other app.php config elements
    'providers' => [
        // Other providers
        // ...
        \Reshadman\FileSecretary\Infrastructure\FileSecretaryServiceProvider::class    
    ]  
];

 return [
    // Other file secretary config elements...
    'contexts' => [
            // Other contexts...
            'assets_context' => [
                'category' => \Reshadman\FileSecretary\Application\ContextCategoryTypes::TYPE_ASSET,
                'driver' => 'rackspace_asset_disk',
                'context_folder' => 'some_context_folder',
                'driver_base_address' => 'some-unique-string.rackcdn.com/etc/',
            ]
    ],
    
    'asset_folders' => [
        'backoffice' => [
            'path' => public_path('backoffice-assets'),
            'context' => 'assets_context',
            // fills .env automatically like => BACKOFFICE_ASSET_ID=unique-id
            // which causes to the browser to not serve old versions.
            'env_key' => 'BACKOFFICE_ASSET_ID',
        ],    
    ]
];


$url = fs_asset('backoffice', 'styles.dist.css');

dump($url);
// In development env: http://localhost:8000/backoffice/styles.dist.css
// In production env: some-unique-id.rackcdn.com/[context_folder]/[latest-unique-id]/styles.dist.css


return [
    // Other config file_secretary config elements
    'listeners' => [
        \Reshadman\FileSecretary\Application\Events\AfterAssetUpload::class => [
            '\YourListener\Class',    
        ]  
    ],  
];



\Reshadman\FileSecretary\Application\PresentedFile::class;

 
return [
    'file_name_generator' => \OpensslRandomFileNameGenerator::class,
      
    // O1ther config elements...
];



$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'file_manager_private', // context name
    'SOME_TEXT_CONTENT_HERE_', // The mime type will be detected automatically.
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_CONTENT
);

/** @var \Reshadman\FileSecretary\Application\Usecases\StoreFile $storeCommand */
$storeCommand = app(\Reshadman\FileSecretary\Application\Usecases\StoreFile::class);

$addressableRemoteFile = $storeCommand->execute($presentedFile);

dd($addressableRemoteFile->fullRelative(), $addressableRemoteFile->fullUrl());




$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'file_manager_private', // context name
    $path = '../path_to/my_file.png',
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_PATH,
    basename($path)  
);



$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'file_manager_private', // context name
    file_get_contents($path = '../path_to/my_file.png'), // The mime type will be detected automatically.
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_CONTENT
);



$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'file_manager_private', // context name
    request()->file('company_logo'), // The mime type will be detected automatically.
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_INSTANCE
);



$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'file_manager_private', // context name
    'https://logo_url.com/logo.png', // The mime type will be detected automatically.
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_URL
);



$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'file_manager_private', // context name
    'base64encodecontent=', // The mime type will be detected automatically.
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_BASE64
);


\Reshadman\FileSecretary\Application\AddressableRemoteFile::class;



/** @var \Reshadman\FileSecretary\Application\Usecases\DeleteFile $deleter */
$deleter = app(\Reshadman\FileSecretary\Application\Usecases\DeleteFile::class);

$fileFullRelativePath = '/context_folder/unique-id.pdf';

$deleter->execute("context_name", $fileFullRelativePath);


 return [
  'companies_logo_200x200' => [
    'class' => \Reshadman\FileSecretary\Infrastructure\Images\Templates\DynamicResizableTemplate::class,
    'args' => [
      'width' => 200,
      'height' => 200,
      'encodings' => null, // When null only parent file encoding is allowed.
      'strip' =>  false, // removes the ICC profile when imagick is used.
    ],
  ],  
];

 
\Reshadman\FileSecretary\Infrastructure\Images\Templates\DynamicResizableTemplate::class


\Reshadman\FileSecretary\Infrastructure\Images\TemplateInterface::class;



\Reshadman\FileSecretary\Application\PrivacyCheckNeeds::class;



$presentedFile = new \Reshadman\FileSecretary\Application\PresentedFile(
    'user_images_context',
    request()->file('avatar_file'),
    \Reshadman\FileSecretary\Application\PresentedFile::FILE_TYPE_INSTANCE,
    basename(request()->file('avatar_file')) // Is stored in the table so you can show the client's name if needed.
);

/** @var \Reshadman\FileSecretary\Application\Usecases\StoreTrackedFile $storeTrackedFile */
$storeTrackedFile = app(\Reshadman\FileSecretary\Application\Usecases\StoreTrackedFile::class);

$trackedModel = $storeTrackedFile->execute($presentedFile);

auth()->user()->avatar()->attach($trackedModel);

dump($trackedModel->full_url);

echo "<img src='{$trackedModel->image_templates['avatar_50x50']}' title='{$trackedModel->original_name}'/>";



\Reshadman\FileSecretary\Application\EloquentPersistedFile::class;

 return [
    // Other file_secretary.php config elements
    'eloquent' => [
        'model' => '\YourOwnModel\Class',
        'table' => 'defined_table_in_model_or_this_string_in_fallback'
    ]  
];



/** @var \Reshadman\FileSecretary\Application\EloquentPersistedFile $trackedFile */
$trackedFile = auth()->user()->avatar;

/** @var \Reshadman\FileSecretary\Application\Usecases\DeleteTrackedFile $deleter */
$deleter = app(\Reshadman\FileSecretary\Application\Usecases\DeleteTrackedFile::class);

$deleter->execute($trackedFile);

// You can also delete a file by uuid
$deleter->execute($trackedFile->getFileableUuid());



use Reshadman\FileSecretary\Application\Usecases\DeleteTrackedFile;

/** @var \Reshadman\FileSecretary\Application\EloquentPersistedFile $trackedFile */
$trackedFile = auth()->user()->avatar;

/** @var \Reshadman\FileSecretary\Application\Usecases\DeleteTrackedFile $deleter */
$deleter = app(\Reshadman\FileSecretary\Application\Usecases\DeleteTrackedFile::class);

$deleter->execute($trackedFile, DeleteTrackedFile::ON_DELETE_DELETE_REMOTE);

$deleter->execute($trackedFile, DeleteTrackedFile::ON_DELETE_IGNORE_REMOTE);

$deleter->execute($trackedFile, DeleteTrackedFile::ON_DELETE_DELETE_IF_NOT_IN_OTHERS);



use Reshadman\FileSecretary\Application\EloquentPersistedFile;
use Reshadman\FileSecretary\Infrastructure\FileSecretaryManager;

class SomeController extends \Illuminate\Routing\Controller {
    
    public function getDocument($fileId, FileSecretaryManager $fManager)
    {
        if (!auth()->user()->is_admin) {
            abort(403);
        }
        
        /** @var EloquentPersistedFile $trackedFile */
        $trackedFile = EloquentPersistedFile::findOrFail($fileId);
        
        $diskDriver = $fManager->getContextDriver($trackedFile->getFileableContext());
        
        $path = $trackedFile->full_relative_path;
        
        $contents = $diskDriver->get($path);
        
        if ($contents === false) {
            abort(404);
        }
        
        $headers = ['Content-Type' => $diskDriver->mimeType($path)];
        
        return response($contents, 200, $headers);
    }
    
}




route('file-secretary.get.download_file', [
    'context_name' => '',
    'context_folder' => '',
    'after_context_path'
]);

 return [
    // Other config elements
    'load_routes' => false  
];



\Reshadman\FileSecretary\Presentation\Http\Actions\DownloadFileAction::class;



\Reshadman\FileSecretary\Presentation\Http\Middleware\PrivacyCheckMiddleware::class;



\Reshadman\FileSecretary\Application\Privacy\PrivacyInterface::class;


\Reshadman\FileSecretary\Presentation\Http\HeadersMutatorInterface::class;
bash
php artisqan vendor:publish \
    --provider=Reshadman\FileSecretary\Infrastructure\FileSecretaryServiceProvider \
    --tag=config
bash
php artisqan vendor:publish \
    --provider=Reshadman\FileSecretary\Infrastructure\FileSecretaryServiceProvider \
    --tag=migrations
bash
php artisan file-secretary:upload-assets --tags=backoffice