public async Task SaveAsync(MediaFile mediaFile, MediaStorageItem item)
        {
            Guard.NotNull(mediaFile, nameof(mediaFile));

            // TODO: (?) if the new file extension differs from the old one then the old file never gets deleted

            var filePath = GetPath(mediaFile);

            if (item != null)
            {
                // Create folder if it does not exist yet
                var dir = Path.GetDirectoryName(filePath);
                if (!_fileSystem.FolderExists(dir))
                {
                    _fileSystem.CreateFolder(dir);
                }

                using (item)
                {
                    using var outStream = _fileSystem.GetFile(filePath).OpenWrite();
                    await item.SaveToAsync(outStream, mediaFile);
                }
            }
            else if (_fileSystem.FileExists(filePath))
            {
                // Remove media storage if any
                _fileSystem.DeleteFile(filePath);
            }
        }
Exemple #2
0
        public async Task ReceiveAsync(MediaMoverContext context, MediaFile mediaFile, Stream stream)
        {
            Guard.NotNull(context, nameof(context));
            Guard.NotNull(mediaFile, nameof(mediaFile));

            // Store data for later bulk commit
            if (stream != null && stream.Length > 0)
            {
                // Requires AutoDetectChanges set to true or remove explicit entity detaching
                await SaveAsync(mediaFile, MediaStorageItem.FromStream(stream));
            }
        }
Exemple #3
0
        public async Task SaveAsync(MediaFile mediaFile, MediaStorageItem item)
        {
            Guard.NotNull(mediaFile, nameof(mediaFile));

            if (item == null)
            {
                mediaFile.ApplyBlob(null);
            }
            else
            {
                await SaveInternalAsync(mediaFile, item);
            }

            _mediaFileRepo.Update(mediaFile);
        }
Exemple #4
0
        public void Save(MediaFile mediaFile, MediaStorageItem item)
        {
            Guard.NotNull(mediaFile, nameof(mediaFile));

            if (item == null)
            {
                mediaFile.ApplyBlob(null);
            }
            else
            {
                SaveInternal(mediaFile, item);
            }

            _mediaFileRepo.Update(mediaFile);
        }
Exemple #5
0
        private void SaveInternal(MediaFile mediaFile, MediaStorageItem item)
        {
            using (item)
            {
                if (_isSqlServer)
                {
                    SaveFast(mediaFile, item);
                }
                else
                {
                    var buffer = item.SourceStream.ToByteArray();
                    mediaFile.ApplyBlob(buffer);
                    mediaFile.Size = buffer.Length;
                }
            }

            _mediaFileRepo.Update(mediaFile);
        }
Exemple #6
0
        private int SaveFast(MediaFile mediaFile, MediaStorageItem item)
        {
            var sourceStream = item.SourceStream;

            mediaFile.Size = (int)sourceStream.Length;

            if (mediaFile.MediaStorageId == null)
            {
                // Insert new blob
                var sql = "INSERT INTO [MediaStorage] (Data) Values(@p0)";
                mediaFile.MediaStorageId = ((DbContext)_dbContext).InsertInto(sql, sourceStream);
                return(mediaFile.MediaStorageId.Value);
            }
            else
            {
                // Update existing blob
                var sql = "UPDATE [MediaStorage] SET [Data] = @p0 WHERE Id = @p1";
                _dbContext.ExecuteSqlCommand(sql, false, null, sourceStream, mediaFile.MediaStorageId.Value);
                return(mediaFile.MediaStorageId.Value);
            }
        }