Пример #1
0
        /// <summary>
        /// Adds or updates the given model in the database
        /// depending on its state.
        /// </summary>
        /// <param name="content">The content to save</param>
        public async Task SaveAsync(MediaContent content)
        {
            if (!App.MediaTypes.IsSupported(content.Filename))
            {
                throw new ValidationException("Filetype not supported.");
            }

            Media model = null;

            if (content.Id.HasValue)
            {
                model = await GetByIdAsync(content.Id.Value).ConfigureAwait(false);
            }

            if (model == null)
            {
                model = new Media()
                {
                    Id      = model != null || content.Id.HasValue ? content.Id.Value : Guid.NewGuid(),
                    Created = DateTime.Now
                };
                content.Id = model.Id;
            }
            else
            {
                using (var session = await _storage.OpenAsync().ConfigureAwait(false))
                {
                    // Delete all versions as we're updating the image
                    if (model.Versions.Count > 0)
                    {
                        foreach (var version in model.Versions)
                        {
                            // Delete version from storage
                            await session.DeleteAsync(model, GetResourceName(model, version.Width, version.Height, version.FileExtension)).ConfigureAwait(false);
                        }
                        model.Versions.Clear();
                    }

                    // Delete the old file because we might have a different filename
                    await session.DeleteAsync(model, GetResourceName(model)).ConfigureAwait(false);
                }
            }

            var type = App.MediaTypes.GetItem(content.Filename);

            model.Filename     = content.Filename.Replace(" ", "_");
            model.FolderId     = content.FolderId;
            model.Type         = App.MediaTypes.GetMediaType(content.Filename);
            model.ContentType  = type.ContentType;
            model.LastModified = DateTime.Now;

            Stream stream = null;

            if (content is BinaryMediaContent binaryContent)
            {
                stream = new MemoryStream(binaryContent.Data);
            }
            else if (content is StreamMediaContent streamContent)
            {
                stream = streamContent.Data;
            }

            // Pre-process if this is an image
            if (_processor != null && type.AllowProcessing && model.Type == MediaType.Image)
            {
                // Make sure to apply auto orientation according to exif
                var memStream = new MemoryStream();
                _processor.AutoOrient(stream, memStream);

                // Get the image size
                _processor.GetSize(memStream, out var width, out var height);
                model.Width  = width;
                model.Height = height;

                stream = memStream;
            }

            // Upload to storage
            using (var session = await _storage.OpenAsync().ConfigureAwait(false))
            {
                model.Size = stream.Length;
                await session.PutAsync(model, model.Filename,
                                       model.ContentType, stream).ConfigureAwait(false);
            }

            App.Hooks.OnBeforeSave(model);
            await _repo.Save(model).ConfigureAwait(false);

            App.Hooks.OnAfterSave(model);

            RemoveFromCache(model);
            RemoveStructureFromCache();
        }