Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShareCodes #8

Merged
merged 2 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions app/Console/Commands/DeleteExpiredFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use App\Models\File;
use App\Models\ShareCode;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

Expand Down Expand Up @@ -44,8 +45,8 @@ public function handle()
Log::info('Deleted file: ' . $file->path . ' for expiry (date): ' . $file->expiry_date);
}
}
if($file->download_limit != 0) {
if($file->download_count >= $file->download_limit) {
if ($file->download_limit != 0) {
if ($file->download_count >= $file->download_limit) {
Storage::disk(config('dropspace.ds_storage_type'))->delete('dropspace/uploads/' . $file->path);
$file->deleted_for_expiry = 1;
$file->save();
Expand All @@ -54,5 +55,18 @@ public function handle()
}
}
Log::info('Finished removing expired files.');
Log::info('Starting removal of expired sharecodes.');
$sharecodes = ShareCode::all();
foreach ($sharecodes as $sharecode) {
$date = Carbon::parse($sharecode->expiry_date);
if ($date->isPast()) {
$sharecode->delete();
Log::info('Deleted sharecode: ' . $sharecode->code . ' for expiry (date): ' . $sharecode->expiry_date);
}
if($sharecode->used == 1) {
$sharecode->delete();
Log::info('Deleted sharecode: ' . $sharecode->code . ' because it has been used.');
}
}
}
}
79 changes: 79 additions & 0 deletions app/Http/Controllers/ShareCodeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ShareCode;
use App\Models\File;

class ShareCodeController extends Controller
{
//
public function inputScreen()
{
return view('sharecode-search');
}

public function findShareCode($id)
{
$sharecode = ShareCode::where('code', strtoupper($id))->first();
if ($sharecode) {
//Check if the sharecode is expired
if ($sharecode->expiry_date < date('Y-m-d H:i:s')) {
return view('download-error')->with('error', 'This ShareCode has expired.');
}
if ($sharecode->used == 1) {
return view('download-error')->with('error', 'This ShareCode has already been used.');
}
$file = File::where('file_identifier', $sharecode->file_identifier)->first();
//return response()->json(['file' => $file, 'sharecode' => $sharecode]);
if($file->is_protected == true) {
$sharecode->used = 1;
$sharecode->save();
return redirect('/download/'.$file->file_identifier.'?hash=' . $file->password);
}else{
$sharecode->used = 1;
$sharecode->save();
return redirect('/download/'.$file->file_identifier);
}
}else{
return view('download-error')->with('error', 'Could not find ShareCode.');
}
}

public function generateShareCode(Request $request)
{
//
//Find file from identifier
$file = File::where('file_identifier', $request->file_id)->first();
if($file == null)
{
return response()->json(['error' => 'File not found'], 404);
}
if($file->is_protected == true)
{
if($request->hash != $file->password){
return response()->json(['error' => 'Wrong password'], 401);
}
else{
$shareCode = new ShareCode();
$shareCode->file_identifier = $request->file_id;
$shareCode->used = false;
$shareCode->expiry_date = date('Y-m-d H:i:s', strtotime('+30 minutes'));
//Generate random code that can contain letters and at least two numbers
$shareCode->code = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyz', mt_rand(1, 10))), 1, 6);
$shareCode->save();
return response()->json(['code' => $shareCode->code, 'expiry_date' => $shareCode->expiry_date]);
}
} else{
$shareCode = new ShareCode();
$shareCode->file_identifier = $request->file_id;
$shareCode->used = false;
$shareCode->expiry_date = date('Y-m-d H:i:s', strtotime('+30 minutes'));
//Generate random code that can contain letters and at least two numbers, length should be 6
$shareCode->code = strtoupper(substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyz', mt_rand(1, 10))), 1, 6));
$shareCode->save();
return response()->json(['code' => $shareCode->code, 'expiry_date' => $shareCode->expiry_date]);
}
}
}
16 changes: 16 additions & 0 deletions app/Models/ShareCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShareCode extends Model
{
use HasFactory;
protected $fillable = ['code',
'file_identifier',
'expiry_date',
'used'
];
}
40 changes: 40 additions & 0 deletions database/migrations/2022_04_30_175128_create_share_codes_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* 'code',
* 'file_identifier',
* 'expiry_date',
* 'used'
*
* @return void
*/
public function up()
{
Schema::create('share_codes', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('code')->unique();
$table->string('file_identifier');
$table->dateTime('expiry_date');
$table->boolean('used')->default(false);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('share_codes');
}
};