Skip to content
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
105 changes: 60 additions & 45 deletions src/Gdrive.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,125 +5,140 @@
use File;
use Illuminate\Support\Facades\Storage;
use Yaza\LaravelGoogleDriveStorage\interfaces\GdriveInterface;
use Yaza\LaravelGoogleDriveStorage\typings\GdriveFile;
use Yaza\LaravelGoogleDriveStorage\typings\GdriveFileInfo;
use \League\Flysystem\DirectoryListing;
use \League\Flysystem\StorageAttributes;

class Gdrive implements GdriveInterface
{
/**
* get file from gdrive
* Get file from gdrive
*
* @return mixed
* @param string $file_path
* @return GdriveFile
*/
public static function get($file_path)
public static function get(string $file_path): GdriveFile
{
$fileinfo = self::getFileInfo($file_path);

$rawData = Storage::disk('google')->get($fileinfo->path);

return (object) [
'file' => $rawData,
'ext' => $fileinfo->ext,
'filename' => $fileinfo->filename,
];
return new GdriveFile(
file: $rawData,
ext: $fileinfo->ext,
filename: $fileinfo->filename,
path: $fileinfo->path
);
}

/**
* read file to stream
* Read file to stream
*
* @return mixed|object
* @param string $filepath
* @return GdriveFile
*/
public static function readStream($filepath)
public static function readStream(string $filepath): GdriveFile
{
$fileinfo = self::getFileInfo($filepath);

$readStream = Storage::disk('google')->getDriver()->readStream($fileinfo->path);

return (object) [
'file' => $readStream,
'ext' => $fileinfo->ext,
'filename' => $fileinfo->filename,
];
return new GdriveFile(
file: $readStream,
ext: $fileinfo->ext,
filename: $fileinfo->filename,
path: $fileinfo->path
);
}

/**
* put file
* Put file
*
* @param $location
* @param bool $random_file_name
* @return mixed|void
* @param string $path
* @param string $file
* @return void
*/
public static function put($path, $file)
public static function put(string $path, string $file): void
{
Storage::disk('google')->put($path, File::get($file));
}

/**
* get file info
* Get file info
*
* @return mixed|object
* @param string $file_path
* @return GdriveFileInfo
*/
public static function getFileInfo($file_path)
public static function getFileInfo(string $file_path): GdriveFileInfo
{
$path = str_replace('\\', '/', $file_path);
$arr = explode('/', $path);
$file_name = end($arr);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);

return (object) [
'filename' => $file_name,
'ext' => $ext,
'path' => $path,
];
return new GdriveFileInfo(
filename: $file_name,
ext: $ext,
path: $path,
);
}

/**
* delete file
* Delete file
*
* @return mixed|void
* @param string $path
* @return void
*/
public static function delete($path)
public static function delete(string $path): void
{
$fileinfo = self::getFileInfo($path);

Storage::disk('google')->delete($fileinfo->path);
}

/**
* make directory
* Make directory
*
* @return mixed|void
* @param string $dirname
* @return void
*/
public static function makeDir($dirname)
public static function makeDir(string $dirname): void
{
Storage::disk('google')->makeDirectory($dirname);
}

/**
* delete directory
* Delete directory
*
* @return mixed|void
* @param string $dirpath
* @return void
*/
public static function deleteDir($dirpath)
public static function deleteDir(string $dirpath): void
{
Storage::disk('google')->deleteDirectory($dirpath);
}

/**
* rename directory
* Rename directory
*
* @return mixed|void
* @param string $dirpath
* @param string $newdirname
* @return void
*/
public static function renameDir($dirpath, $newdirname)
public static function renameDir(string $dirpath, string $newdirname): void
{
Storage::disk('google')->move($dirpath, $newdirname);
}

/**
* all folder
* All folder
*
* @param bool $recursive
* @return mixed
* @param string $path
* @param bool $recursive
* @return DirectoryListing<StorageAttributes>
*/
public static function all($path, $recursive = true)
public static function all(string $path, bool $recursive = true)
{
$contents = collect(Storage::disk('google')->listContents($path, $recursive));

Expand Down
71 changes: 44 additions & 27 deletions src/interfaces/GdriveInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,85 @@

namespace Yaza\LaravelGoogleDriveStorage\interfaces;

use Yaza\LaravelGoogleDriveStorage\typings\GdriveFile;
use Yaza\LaravelGoogleDriveStorage\typings\GdriveFileInfo;
use \League\Flysystem\DirectoryListing;
use \League\Flysystem\StorageAttributes;

interface GdriveInterface
{
/**
* get file
* Get file
*
* @return mixed
* @param string $file_path
* @return GdriveFile
*/
public static function get($file_path);
public static function get(string $file_path);

/**
* read file with stream
* Read file with stream
*
* @return mixed
* @param string $filepath
* @return GdriveFile
*/
public static function readStream($filepath);
public static function readStream(string $filepath);

/**
* put file
* Put file
*
* @return mixed
* @param string $path
* @param string $file
* @return void
*/
public static function put($path, $file);
public static function put(string $path, string $file);

/**
* delete file
* Delete file
*
* @return mixed
* @param string $path
* @return void
*/
public static function delete($path);
public static function delete(string $path);

/**
* make directory
* Make directory
*
* @return mixed
* @param string $dirname
* @return void
*/
public static function makeDir($dirname);
public static function makeDir(string $dirname);

/**
* delete directory
* Delete directory
*
* @return mixed
* @param string $dirpath
* @return void
*/
public static function deleteDir($dirpath);
public static function deleteDir(string $dirpath);

/**
* rename directory
* Rename directory
*
* @return mixed
* @param string $dirpath
* @param string $newdirname
* @return void
*/
public static function renameDir($dirpath, $newdirname);
public static function renameDir(string $dirpath, string $newdirname);

/**
* get file info
* Get file info
*
* @return mixed
* @param string $path
* @return GdriveFileInfo
*/
public static function getFileInfo($path);
public static function getFileInfo(string $path);

/**
* all folder & file
* All folder & file
*
* @return mixed
* @param string $path
* @param bool $recursive
* @return DirectoryListing<StorageAttributes>
*/
public static function all($path, $recursive);
public static function all(string $path, bool $recursive);
}
13 changes: 13 additions & 0 deletions src/typings/GdriveFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Yaza\LaravelGoogleDriveStorage\typings;

class GdriveFile
{
public function __construct(
public string|null $file,
public array|string $ext,
public string $filename,
public string $path,
) {}
}
12 changes: 12 additions & 0 deletions src/typings/GdriveFileInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Yaza\LaravelGoogleDriveStorage\typings;

class GdriveFileInfo
{
public function __construct(
public array|string $ext,
public string $filename,
public string $path,
) {}
}