4

我正在努力通过 PHP 导入媒体以使 Shopware 6 正常工作。

这是我的服务:

<?php declare(strict_types=1);

namespace My\Namespace\Service;

use Shopware\Core\Content\Media\File\MediaFile;
use Shopware\Core\Content\Media\MediaService;
use Shopware\Core\Framework\Context;

class ImageImport
{
    /**
     * @var MediaService
     */
    protected $mediaService;

    /**
     * ImageImport constructor.
     * @param MediaService $mediaService
     */
    public function __construct(MediaService $mediaService)
    {
        $this->mediaService = $mediaService;
    }


    public function addImageToProductMedia($imageUrl, Context $context)
    {
        $mediaId = NULL;
        $context->disableCache(function (Context $context) use ($imageUrl, &$mediaId): void {
            $filePathParts = explode('/', $imageUrl);
            $fileName = array_pop($filePathParts);
            $fileNameParts = explode('.', $fileName);

            $actualFileName = $fileNameParts[0];
            $fileExtension = $fileNameParts[1];


            if ($actualFileName && $fileExtension) {
                $tempFile = tempnam(sys_get_temp_dir(), 'image-import');
                file_put_contents($tempFile, file_get_contents($imageUrl));

                $fileSize = filesize($tempFile);
                $mimeType = mime_content_type($tempFile);

                $mediaFile = new MediaFile($tempFile, $mimeType, $fileExtension, $fileSize);

                $mediaId = $this->mediaService->saveMediaFile($mediaFile, $actualFileName, $context, 'product');
            }
        });
        return $mediaId;
    }
}

将在表 media 中创建一个具有正确 media_folder_association 的条目。据我所见,与通过后端上传的其他媒体没有区别(私有为 1 且 user_id 为 NULL 除外)。

但是在后端,媒体条目已损坏,似乎无法加载实际的图像文件(我尝试将私有设置为 true 以在媒体部分看到它,通过 php 将媒体添加到产品时也会发生同样的情况,但我想问题出在对产品进行任何分配之前)。

后端媒体中的图像

有人建议这里有什么问题吗?

谢谢菲尔

===== 解决方案 ======

这是更新和工作的服务:

<?php declare(strict_types=1);

namespace My\Namespace\Service;

use Shopware\Core\Content\Media\File\FileSaver;
use Shopware\Core\Content\Media\File\MediaFile;
use Shopware\Core\Content\Media\MediaService;
use Shopware\Core\Framework\Context;

class ImageImport
{
    /**
     * @var MediaService
     */
    protected $mediaService;

    /**
     * @var FileSaver
     */
    private $fileSaver;

    /**
     * ImageImport constructor.
     * @param MediaService $mediaService
     * @param FileSaver $fileSaver
     */
    public function __construct(MediaService $mediaService, FileSaver $fileSaver)
    {
        $this->mediaService = $mediaService;
        $this->fileSaver = $fileSaver;
    }


    public function addImageToProductMedia($imageUrl, Context $context)
    {
        $mediaId = NULL;
        $context->disableCache(function (Context $context) use ($imageUrl, &$mediaId): void {
            $filePathParts = explode('/', $imageUrl);
            $fileName = array_pop($filePathParts);
            $fileNameParts = explode('.', $fileName);

            $actualFileName = $fileNameParts[0];
            $fileExtension = $fileNameParts[1];


            if ($actualFileName && $fileExtension) {
                $tempFile = tempnam(sys_get_temp_dir(), 'image-import');
                file_put_contents($tempFile, file_get_contents($imageUrl));

                $fileSize = filesize($tempFile);
                $mimeType = mime_content_type($tempFile);

                $mediaFile = new MediaFile($tempFile, $mimeType, $fileExtension, $fileSize);
                $mediaId = $this->mediaService->createMediaInFolder('product', $context, false);

                $this->fileSaver->persistFileToMedia(
                    $mediaFile,
                    $actualFileName,
                    $mediaId,
                    $context
                );
            }
        });
        return $mediaId;
    }
}
4

1 回答 1

0

为了将文件导入 Shopware 6,需要执行两个步骤:

  1. 您必须创建一个媒体文件对象 ( MediaDefinition / media table)。看看 MediaConverter
  2. SwagMigrationMediaFileDefinition在(swag_migration_media_file表)中创建一个新条目。

相关迁移运行的表中的每个条目swag_migration_media_file都将由 MediaFileProcessorInterface 的实现处理。

要将文件添加到表中,您可以在Converter类中执行以下操作(此示例来自 MediaConverter):

abstract class MediaConverter extends ShopwareConverter
{

    public function convert(
        array $data,
        Context $context,
        MigrationContextInterface $migrationContext
    ): ConvertStruct {
        $this->generateChecksum($data);
        $this->context = $context;
        $this->locale = $data['_locale'];
        unset($data['_locale']);

        $connection = $migrationContext->getConnection();
        $this->connectionId = '';
        if ($connection !== null) {
            $this->connectionId = $connection->getId();
        }

        $converted = [];
        $this->mainMapping = $this->mappingService->getOrCreateMapping(
            $this->connectionId,
            DefaultEntities::MEDIA,
            $data['id'],
            $context,
            $this->checksum
        );
        $converted['id'] = $this->mainMapping['entityUuid'];

        if (!isset($data['name'])) {
            $data['name'] = $converted['id'];
        }

        $this->mediaFileService->saveMediaFile(
            [
                'runId' => $migrationContext->getRunUuid(),
                'entity' => MediaDataSet::getEntity(), // important to distinguish between private and public files
                'uri' => $data['uri'] ?? $data['path'],
                'fileName' => $data['name'], // uri or path to the file (because of the different implementations of the gateways)
                'fileSize' => (int) $data['file_size'],
                'mediaId' => $converted['id'], // uuid of the media object in Shopware 6
            ]
        );
        unset($data['uri'], $data['file_size']);

        $this->getMediaTranslation($converted, $data);
        $this->convertValue($converted, 'title', $data, 'name');
        $this->convertValue($converted, 'alt', $data, 'description');

        $albumMapping = $this->mappingService->getMapping(
            $this->connectionId,
            DefaultEntities::MEDIA_FOLDER,
            $data['albumID'],
            $this->context
        );

        if ($albumMapping !== null) {
            $converted['mediaFolderId'] = $albumMapping['entityUuid'];
            $this->mappingIds[] = $albumMapping['id'];
        }

        unset(
            $data['id'],
            $data['albumID'],

            // Legacy data which don't need a mapping or there is no equivalent field
            $data['path'],
            $data['type'],
            $data['extension'],
            $data['file_size'],
            $data['width'],
            $data['height'],
            $data['userID'],
            $data['created']
        );

        $returnData = $data;
        if (empty($returnData)) {
            $returnData = null;
        }
        $this->updateMainMapping($migrationContext, $context);

        // The MediaWriter will write this Shopware 6 media object
        return new ConvertStruct($converted, $returnData, $this->mainMapping['id']);
    }

}

swag_migration_media_files由正确的处理器服务处理。该服务对于文档和普通媒体是不同的,但它仍然依赖于网关

=== 不同的方法(Shyim 建议)===

看看这个(取自 Shopwaredowntown 的 Github 存储库):

public function upload(UploadedFile $file, string $folder, string $type, Context $context): string
    {
        $this->checkValidFile($file);

        $this->validator->validate($file, $type);

        $mediaFile = new MediaFile($file->getPathname(), $file->getMimeType(), $file->getClientOriginalExtension(), $file->getSize());

        $mediaId = $this->mediaService->createMediaInFolder($folder, $context, false);

        try {
            $this->fileSaver->persistFileToMedia(
                $mediaFile,
                pathinfo($file->getFilename(), PATHINFO_FILENAME),
                $mediaId,
                $context
            );
        } catch (MediaNotFoundException $e) {
            throw new UploadException($e->getMessage());
        }

        return $mediaId;
    }

src/Portal/Hacks/StorefrontMediaUploader.php:49

public function upload(UploadedFile $file, string $folder, string $type, Context $context): string
于 2020-08-14T10:05:14.573 回答