-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: documents (monicahq/chandler#159)
- Loading branch information
Showing
70 changed files
with
2,552 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class EnvVariablesNotSetException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
class FileHelper | ||
{ | ||
/** | ||
* Formats the file size to a human readable size. | ||
* | ||
* @param int $bytes | ||
* @return string|null | ||
*/ | ||
public static function formatFileSize(int $bytes): ?string | ||
{ | ||
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
$step = 1024; | ||
$i = 0; | ||
while (($bytes / $step) > 0.9) { | ||
$bytes = $bytes / $step; | ||
$i++; | ||
} | ||
|
||
return round($bytes, 2) . $units[$i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use App\Models\Account; | ||
use App\Models\Contact; | ||
use App\Models\File; | ||
|
||
class StorageHelper | ||
{ | ||
/** | ||
* Indicate if the account can accept another file, depending on the account's | ||
* limits. | ||
* | ||
* @param Account $account | ||
* @return bool | ||
*/ | ||
public static function canUploadFile(Account $account): bool | ||
{ | ||
// get the file size of all the files in the account | ||
// the size will be in bytes | ||
$vaultIds = $account->vaults()->select('id')->get()->pluck('id')->toArray(); | ||
$contactIds = Contact::whereIn('vault_id', $vaultIds)->select('id')->get()->pluck('id')->toArray(); | ||
|
||
$totalSizeInBytes = File::whereIn('contact_id', $contactIds)->sum('size'); | ||
|
||
$accountLimit = $account->storage_limit_in_mb * 1024 * 1024; | ||
|
||
return $totalSizeInBytes < $accountLimit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Contact\ManageDocuments\Events\FileDeleted; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class File extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'files'; | ||
|
||
/** | ||
* Possible type. | ||
*/ | ||
public const TYPE_DOCUMENT = 'document'; | ||
public const TYPE_AVATAR = 'avatar'; | ||
public const TYPE_PHOTO = 'photo'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'contact_id', | ||
'uuid', | ||
'original_url', | ||
'cdn_url', | ||
'name', | ||
'mime_type', | ||
'type', | ||
'size', | ||
]; | ||
|
||
/** | ||
* The event map for the model. | ||
* | ||
* @var array | ||
*/ | ||
protected $dispatchesEvents = [ | ||
'deleted' => FileDeleted::class, | ||
]; | ||
|
||
/** | ||
* Get the contact associated with the file. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function contact(): BelongsTo | ||
{ | ||
return $this->belongsTo(Contact::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.