nao-pon/flysystem-google-drive

File is corrupted after uploading to google drive. all files are the same size.

Soel30 opened this issue · 0 comments

so I'm making a function to extract all the files in the zip then put them in the storage folder, then loop to get the folder name and file name that has been extracted.

After I managed to get it, I uploaded the file to Google Drive. to upload the file successfully, but the file becomes corrupt and cannot be used. this my code

 public function unzip($file, $post, $req)
    {
        $unzipper  = new Unzip();
        $file = $file->store('public/manga'); //store file in storage/app/zip
        $unzipper->extract(storage_path('app/' . $file), storage_path('app/public/manga'));
        Storage::disk('google')->makeDirectory($post->title);
        $mgDir = Storage::disk('google')->getMetadata($post->title);

        $directories = Storage::allDirectories('/public/manga');
        foreach ($directories as $dir) {
            $files = Storage::allFiles('/public/manga/' . explode('/', $dir)[2]);
            $ch = Chapter::create([
                'title' =>  explode('/', $dir)[2],
                'slug' => Str::slug($post->title . '-' .  explode('/', $dir)[2] . '-' . Str::random(5)),
                'post_id' => $post->id
            ]);
            $no = 1;
            Storage::disk('google')->makeDirectory($mgDir['path'] . '/' . $ch->title);
            $chDir = Storage::disk('google')->getMetadata($mgDir['path'] . '/' . $ch->title);

            foreach ($files as $f) {
                $url = storage_path('app/' . $f);
                $fileName = $no . '.' . explode('.', explode('/', $f)[3])[1];
                Storage::disk('google')->put($chDir['path'] . '/' . $fileName, $url);

                $fileID = Storage::disk('google')->getMetadata($chDir['path'] . '/' . $fileName);
                ChapterImg::create([
                    'url' => 'https://drive.google.com/uc?id=' . explode('/', $fileID['path'])[1],
                    'chapter_id' => $ch->id
                ]);
                $no++;
            }
        }
    }