Code Monkey home page Code Monkey logo

Comments (4)

mbaxter91288 avatar mbaxter91288 commented on August 24, 2024 1

@erikn69 ok so I checked the production version and that's v2.2.0 so I downgraded locally and the file not found issue I am having was resolved, however, I then did get the Properties can not be manipulated when useHasDir issue so I manually applied your fixed to the vendor files for now and included a if (!Storage::disk('google')->fileExists($fileId)) continue; in my code and it runs successfully. It looks like all the files are synced perfectly!

Is there anything else I can test to help debug?

from flysystem-google-drive-ext.

erikn69 avatar erikn69 commented on August 24, 2024

Wich laravel version?, package version? php version? give all the info

This has been working fine but now I just can't get things to work

Did you upgrade the package? still almost no info
if you upgrade, test with the previus version

I did try with the next test and everything is working

/**
 * @test
 */
public function testing_file_exist()
{
    $this->runScenario(function () {
        $adapter = $this->adapter();
        $adapter->write('test/file1.txt', 'contents', new Config());
        $adapter->write('test/file2.txt', 'contents', new Config());
        $adapter->write('test/file3.txt', 'contents', new Config());
        
        $this->assertEquals(true, $adapter->fileExists('test/file1.txt'));
        $this->assertEquals(true, $adapter->fileExists('test/file2.txt'));
        $this->assertEquals(true, $adapter->fileExists('test/file3.txt'));
    });
}

from flysystem-google-drive-ext.

mbaxter91288 avatar mbaxter91288 commented on August 24, 2024

@erikn69 thanks for responding so quick! Sorry for lack of information, here's a little more:

PHP Version 8.1.3
"name": "masbug/flysystem-google-drive-ext",
"version": "v2.2.1",

"name": "laravel/framework",
"version": "v9.13.0",

The code is currently working in production. Today I created a new branch locally to upgrade to Nova 4 which I did successfully. I have a custom Tool in Nova that can be triggered by admins which basically syncs the Google drive files so I was reimplementing it from a Tool to an Action.

It was giving me a weird PDOException: There is no active transaction which took me down a huge rabbit hole of trial and error debugging. I eventually noticed that when I was getting the file metadata to store in the DB it was throwing this error, I think, because it couldn't find the file in the first place.

Here is the production code that now no longer seems to work:

<?php

namespace App\Actions\Google;

use Carbon\Carbon;
use App\Models\MarketingMaterial;
use Illuminate\Support\Facades\Cache;
use App\Models\MarketingMaterialFolder;
use Illuminate\Support\Facades\Storage;
use Lorisleiva\Actions\Concerns\AsAction;
use App\Actions\Google\GetMarketingFolderNameFromDisplayNameParts;
use App\Actions\Google\GetMarketingFolderParentIdFromDisplayNameParts;

class SyncFilesAndFoldersFromGoogle
{
    use AsAction;

    public function handle(string $folderName = '') : void
    {

        if ($folderName === '') {
            $this->clearDatabaseAndCache();
        }

        foreach (Storage::cloud()->files($folderName) as $fileId) {

            $meta = Storage::cloud()->getAdapter()->getMetadata($fileId);
            $extra = $meta->extraMetadata();

            $parts = explode('/', $extra['display_path']);
    
            $file = MarketingMaterial::create([
                'id' => $extra['id'],
                'name' => $extra['filename'] . '.' . $extra['extension'],
                'filename' => $extra['filename'] . '.' . $extra['extension'],
                'extension' => $extra['extension'],
                'modified_at' => Carbon::parse($meta['lastModified']),
                'mimetype' => $meta['mimeType'],
                'size' => $meta['fileSize'],
                'description' => '',
                'path' => $meta['path'],
                'folder_id' => GetMarketingFolderParentIdFromDisplayNameParts::run($parts),
            ]);

            if (!Storage::disk('thumbnails')->exists($file->getThumbnailFilename())) {
                DownloadFileThumbnailFromGoogle::run( $file );
            }
    
    
        }
        
        foreach (Storage::cloud()->directories($folderName) as $directoryName) {

            // $meta = Storage::cloud()->
            $meta = Storage::cloud()->getAdapter()->getMetadata($directoryName)->extraMetadata();

            // the new Google Drive implementation returns display names so
            // explode it to get it into parts and we'll deal with what we need from there
            $parts = explode('/', $meta['display_path']);

            MarketingMaterialFolder::create([
                'id' => $meta['id'],
                'name' => GetMarketingFolderNameFromDisplayNameParts::run($parts),
                'path' => $meta['display_path'],
                'description' => '',
                'folder_id' => GetMarketingFolderParentIdFromDisplayNameParts::run($parts),
            ]);
    
            SyncFilesAndFoldersFromGoogle::run($directoryName);
    
        }

    }

    /**
     * Clear both the database of MarketingMaterials and MarketingMaterialFolders
     * plus get rid of the cache of marketing view data by tags
     *
     * @return void
     */
    private function clearDatabaseAndCache() : void
    {

        // clear out all existing files and folders
        MarketingMaterial::truncate();
        MarketingMaterialFolder::truncate();

        // clear marketing folder view cache data
        Cache::tags('marketing-folder-data')->flush();

    }

}

This code currently works in Production but now locally it will sync the first file, but nothing else. I've tried adding a "if fileExists" check and then if it doesn't, just skip that file and continue - this stops the odd 'no transaction' error but nothing syncs down apart from the first file.

After Googling further, I decided to create a fresh Laravel install today, install the package and create the simple test in the original post which still shows it not finding the file after a subsequent request.

I a fully aware I am probably doing something stupid, but I just can't see it :(

from flysystem-google-drive-ext.

erikn69 avatar erikn69 commented on August 24, 2024

test with the previus version

from flysystem-google-drive-ext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.