Преглед изворни кода

Prefill icon field with imageLink resource

Bubka пре 4 година
родитељ
комит
101a26b035

+ 24 - 0
app/Http/Controllers/QrCodeController.php

@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
 use Zxing\QrReader;
 use App\TwoFAccount;
 use App\Classes\Options;
+use Illuminate\Support\Str;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Storage;
 use chillerlan\QRCode\{QRCode, QROptions};
@@ -71,6 +72,29 @@ class QrCodeController extends Controller
         $twofaccount = new TwoFAccount;
         $twofaccount->uri = $uri;
 
+        // When present, use the imageLink parameter to prefill the icon field
+        if( $twofaccount->imageLink ) {
+
+            $chunks = explode('.', $twofaccount->imageLink);
+            $hashFilename = Str::random(40) . '.' . end($chunks);
+
+            try {
+
+                Storage::disk('local')->put('imagesLink/' . $hashFilename, file_get_contents($twofaccount->imageLink));
+
+                if( in_array(Storage::mimeType('imagesLink/' . $hashFilename), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp']) ) {
+                    if( getimagesize(storage_path() . '/app/imagesLink/' . $hashFilename) ) {
+
+                        Storage::move('imagesLink/' . $hashFilename, 'public/icons/' . $hashFilename);
+                        $twofaccount->icon = $hashFilename;
+                    }
+                }
+            }
+            catch( Exception $e ) {
+                $twofaccount->imageLink = null;
+            }
+        }
+
         return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
     }
     

+ 0 - 2
app/Http/Controllers/TwoFAccountController.php

@@ -44,7 +44,6 @@ class TwoFAccountController extends Controller
             'algorithm' => 'nullable|in:sha1,sha256,sha512,md5',
             'totpPeriod' => 'nullable|integer|min:1',
             'hotpCounter' => 'nullable|integer|min:0',
-            'imageLink' => 'nullable|url',
         ]);
 
         // Two possible cases :
@@ -187,7 +186,6 @@ class TwoFAccountController extends Controller
             'algorithm' => 'nullable|in:sha1,sha256,sha512,md5',
             'totpPeriod' => 'required_if:otpType,totp|nullable|integer|min:1',
             'hotpCounter' => 'required_if:otpType,hotp|nullable|integer|min:0',
-            'imageLink' => 'nullable|url',
         ]);
 
         // Here we catch a possible missing model exception in order to

+ 0 - 3
app/TwoFAccount.php

@@ -164,9 +164,6 @@ class TwoFAccount extends Model implements Sortable
             if (array_key_exists('hotpCounter', $attrib) && $attrib['hotpCounter'] && $attrib['otpType'] === 'hotp')
                 { $this->otp->setParameter( 'counter', (int) $attrib['hotpCounter'] ); }
 
-            if (array_key_exists('imageLink', $attrib) && $attrib['imageLink'])
-                { $this->otp->setParameter( 'image', $attrib['imageLink'] ); }
-
         }
         catch (\Exception $e) {
             throw \Illuminate\Validation\ValidationException::withMessages([

+ 2 - 2
resources/js/views/twofaccounts/Create.vue

@@ -111,8 +111,6 @@
                 <form-field v-if="form.otpType === 'totp'" :form="form" fieldName="totpPeriod" inputType="text" :label="$t('twofaccounts.forms.totpPeriod.label')" :placeholder="$t('twofaccounts.forms.totpPeriod.placeholder')" :help="$t('twofaccounts.forms.totpPeriod.help')" />
                 <!-- HOTP counter -->
                 <form-field v-if="form.otpType === 'hotp'" :form="form" fieldName="hotpCounter" inputType="text" :label="$t('twofaccounts.forms.hotpCounter.label')" :placeholder="$t('twofaccounts.forms.hotpCounter.placeholder')" :help="$t('twofaccounts.forms.hotpCounter.help')" />
-                <!-- image link -->
-                <form-field :form="form" fieldName="imageLink" inputType="text" :label="$t('twofaccounts.forms.image_link.label')" :placeholder="$t('twofaccounts.forms.image_link.placeholder')" :help="$t('twofaccounts.forms.image_link.help')" />
             </div>
             <vue-footer :showButtons="true">
                 <p class="control">
@@ -183,6 +181,7 @@
             if( this.$route.params.qrAccount ) {
 
                 this.form.fill(this.$route.params.qrAccount)
+                this.tempIcon = this.$route.params.qrAccount.icon ? this.$route.params.qrAccount.icon : null
                 this.isQuickForm = true
 
             }
@@ -241,6 +240,7 @@
 
                 this.form.fill(data)
                 this.form.secretIsBase32Encoded = 1
+                this.tempIcon = data.icon ? data.icon : null
                 this.form.uri = '' // we don't want the uri because the user can change any otp parameter in the form
 
             },

+ 0 - 2
resources/js/views/twofaccounts/Edit.vue

@@ -83,8 +83,6 @@
                     <field-error :form="form" field="uri" class="help-for-file" />
                     <p class="help" v-html="$t('twofaccounts.forms.hotpCounter.help_lock')"></p>
                 </div>
-                <!-- image link -->
-                <form-field :form="form" fieldName="imageLink" inputType="text" :label="$t('twofaccounts.forms.image_link.label')" :placeholder="$t('twofaccounts.forms.image_link.placeholder')" :help="$t('twofaccounts.forms.image_link.help')" />
             </div>
             <!-- form buttons -->
             <vue-footer :showButtons="true">

+ 1 - 0
storage/app/.gitignore

@@ -1,4 +1,5 @@
 *
+!imagesLink/
 !public/
 !qrcodes/
 !.gitignore

+ 2 - 0
storage/app/imagesLink/.gitignore

@@ -0,0 +1,2 @@
+*
+!.gitignore