Ejemplo n.º 1
0
        private async Task ProcessOneFileAsync(Photo sourcePhoto, Photo targetPhoto, bool rebuild, bool rebuildMetadata, string url, string shortUrl, IImageSettings imageImageSettings)
        {
            this._logging.LogInformation(rebuild ? $"Rebuild: {sourcePhoto.UrlSafePath}" : $"Build: {sourcePhoto.UrlSafePath}");

            await targetPhoto.UpdateFileHashesAsync(sourcePhoto : sourcePhoto, settings : this._settings);

            bool buildMetadata = targetPhoto == null || rebuild || rebuildMetadata || targetPhoto.Metadata == null;

            if (buildMetadata)
            {
                sourcePhoto.Metadata = MetadataExtraction.ExtractMetadata(sourcePhoto: sourcePhoto, settings: this._settings);
            }
            else
            {
                sourcePhoto.Metadata = targetPhoto.Metadata;
            }

            bool buildImages = targetPhoto == null || rebuild || !targetPhoto.ImageSizes.HasAny();

            List <string> filesCreated = new();

            if (buildImages)
            {
                this._logging.LogInformation(message: "Build images:");
                DateTime creationDate = MetadataHelpers.ExtractCreationDate(sourcePhoto.Metadata);

                try
                {
                    IReadOnlyList <ImageSize> sizes = await this._imageExtraction.BuildImagesAsync(sourcePhoto : sourcePhoto,
                                                                                                   filesCreated : filesCreated,
                                                                                                   creationDate : creationDate,
                                                                                                   url : url,
                                                                                                   shortUrl : shortUrl,
                                                                                                   imageSettings : imageImageSettings);

                    sourcePhoto.ImageSizes = sizes.ToList();
                }
                catch (Exception exception)
                {
                    this._logging.LogInformation($" Failed to load image: {sourcePhoto.UrlSafePath}: {exception.Message}");

                    throw;
                }
            }
            else
            {
                this._logging.LogInformation(message: "Not building images");
                sourcePhoto.ImageSizes = targetPhoto.ImageSizes;
            }

            sourcePhoto.Version = Constants.CURRENT_METADATA_VERSION;

            if (targetPhoto != null)
            {
                targetPhoto.UpdateTargetWithSourceProperties(sourcePhoto);
                targetPhoto.Version = Constants.CURRENT_METADATA_VERSION;

                if (buildImages)
                {
                    //?
                }

                await PhotoMetadataRepository.StoreAsync(photo : targetPhoto, databaseOutputFolder : this._settings.DatabaseOutputFolder);
            }
            else
            {
                await PhotoMetadataRepository.StoreAsync(photo : sourcePhoto, databaseOutputFolder : this._settings.DatabaseOutputFolder);
            }
        }