public void Should_download_with_id_and_version()
        {
            sut.DownloadAsync(id, 1, string.Empty, stream);

            A.CallTo(() => sut.DownloadAsync($"{id}_1", stream, default))
            .MustHaveHappened();
        }
Exemplo n.º 2
0
        public IActionResult GetImage(string app)
        {
            if (App.Image == null)
            {
                return(NotFound());
            }

            var etag = App.Image.Etag;

            Response.Headers[HeaderNames.ETag] = etag;

            var handler = new Func <Stream, Task>(async bodyStream =>
            {
                var assetId        = App.Id.ToString();
                var assetResizedId = $"{assetId}_{etag}_Resized";

                try
                {
                    await assetStore.DownloadAsync(assetResizedId, bodyStream);
                }
                catch (AssetNotFoundException)
                {
                    using (Profiler.Trace("Resize"))
                    {
                        using (var sourceStream = GetTempStream())
                        {
                            using (var destinationStream = GetTempStream())
                            {
                                using (Profiler.Trace("ResizeDownload"))
                                {
                                    await assetStore.DownloadAsync(assetId, sourceStream);
                                    sourceStream.Position = 0;
                                }

                                using (Profiler.Trace("ResizeImage"))
                                {
                                    await assetThumbnailGenerator.CreateThumbnailAsync(sourceStream, destinationStream, 150, 150, "Crop");
                                    destinationStream.Position = 0;
                                }

                                using (Profiler.Trace("ResizeUpload"))
                                {
                                    await assetStore.UploadAsync(assetResizedId, destinationStream);
                                    destinationStream.Position = 0;
                                }

                                await destinationStream.CopyToAsync(bodyStream);
                            }
                        }
                    }
                }
            });

            return(new FileCallbackResult(App.Image.MimeType, null, true, handler));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetAssetContent(Guid id, [FromQuery] long version = EtagVersion.Any, [FromQuery] int?width = null, [FromQuery] int?height = null, [FromQuery] string mode = null)
        {
            var entity = await assetRepository.FindAssetAsync(id);

            if (entity == null || entity.FileVersion < version || width == 0 || height == 0)
            {
                return(NotFound());
            }

            Response.Headers["ETag"] = entity.FileVersion.ToString();

            return(new FileCallbackResult(entity.MimeType, entity.FileName, async bodyStream =>
            {
                var assetId = entity.Id.ToString();

                if (entity.IsImage && (width.HasValue || height.HasValue))
                {
                    var assetSuffix = $"{width}_{height}_{mode}";

                    try
                    {
                        await assetStore.DownloadAsync(assetId, entity.FileVersion, assetSuffix, bodyStream);
                    }
                    catch (AssetNotFoundException)
                    {
                        using (var sourceStream = GetTempStream())
                        {
                            using (var destinationStream = GetTempStream())
                            {
                                await assetStore.DownloadAsync(assetId, entity.FileVersion, null, sourceStream);
                                sourceStream.Position = 0;

                                await assetThumbnailGenerator.CreateThumbnailAsync(sourceStream, destinationStream, width, height, mode);
                                destinationStream.Position = 0;

                                await assetStore.UploadAsync(assetId, entity.FileVersion, assetSuffix, destinationStream);
                                destinationStream.Position = 0;

                                await destinationStream.CopyToAsync(bodyStream);
                            }
                        }
                    }
                }
                else
                {
                    await assetStore.DownloadAsync(assetId, entity.FileVersion, null, bodyStream);
                }
            }));
        }
        public async Task DownloadAsync(DomainId appId, DomainId id, long fileVersion, Stream stream, BytesRange range = default, CancellationToken ct = default)
        {
            try
            {
                var fileNameNew = GetFileName(appId, id, fileVersion);

                await assetStore.DownloadAsync(fileNameNew, stream, range, ct);
            }
            catch (AssetNotFoundException)
            {
                var fileNameOld = GetFileName(id, fileVersion);

                await assetStore.DownloadAsync(fileNameOld, stream, range, ct);
            }
        }
Exemplo n.º 5
0
        public async Task Should_invoke_asset_store_to_download_picture()
        {
            A.CallTo(() => assetStore.DownloadAsync(userId, 0, "picture", A <Stream> .Ignored))
            .Invokes(async(string id, long version, string suffix, Stream stream) =>
            {
                await stream.WriteAsync(new byte[] { 1, 2, 3, 4 }, 0, 4);
            });

            var result = await sut.DownloadAsync(userId);

            Assert.Equal(0, result.Position);
            Assert.Equal(4, result.Length);

            A.CallTo(() => assetStore.DownloadAsync(userId, 0, "picture", A <Stream> .Ignored)).MustHaveHappened();
        }
Exemplo n.º 6
0
        public async Task Should_invoke_asset_store_to_download_archive()
        {
            var stream = new MemoryStream();

            await sut.DownloadAsync(appId, stream);

            A.CallTo(() => assetStore.DownloadAsync(fileName, stream, default, CancellationToken.None))
Exemplo n.º 7
0
        public async Task <LuceneDirectory> CreateDirectoryAsync(Guid schemaId)
        {
            var directoryInfo = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "LocalIndices", schemaId.ToString()));

            if (directoryInfo.Exists)
            {
                directoryInfo.Delete(true);
            }

            directoryInfo.Create();

            using (var fileStream = GetArchiveStream(directoryInfo))
            {
                try
                {
                    await assetStore.DownloadAsync(directoryInfo.Name, fileStream);

                    fileStream.Position = 0;

                    using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read, true))
                    {
                        zipArchive.ExtractToDirectory(directoryInfo.FullName);
                    }
                }
                catch (AssetNotFoundException)
                {
                }
            }

            var directory = FSDirectory.Open(directoryInfo);

            return(directory);
        }
        public async Task Should_invoke_asset_store_to_download_archive_using_suffix_for_compatibility()
        {
            var stream = new MemoryStream();

            await sut.DownloadAsync(backupId, stream, ct);

            A.CallTo(() => assetStore.DownloadAsync(fileName, stream, default, ct))
Exemplo n.º 9
0
        public async Task Should_invoke_asset_store_to_download_picture_using_suffix_for_compatibility()
        {
            var stream = new MemoryStream();

            await sut.DownloadAsync(userId, stream);

            A.CallTo(() => assetStore.DownloadAsync(file, stream, default, CancellationToken.None))
Exemplo n.º 10
0
 private Task WriteAssetAsync(Guid assetId, long fileVersion, BackupWriter writer)
 {
     return(writer.WriteBlobAsync(GetName(assetId, fileVersion), stream =>
     {
         return assetStore.DownloadAsync(assetId.ToString(), fileVersion, null, stream);
     }));
 }
Exemplo n.º 11
0
 public IActionResult GetBackupContent(string app, Guid id)
 {
     return(new FileCallbackResult("application/zip", "Backup.zip", bodyStream =>
     {
         return assetStore.DownloadAsync(id.ToString(), 0, null, bodyStream);
     }));
 }
Exemplo n.º 12
0
        public Task DownloadAsync(string appId, Media media, Stream stream, BytesRange range,
                                  CancellationToken ct = default)
        {
            var fileName = CreateFileName(appId, media);

            return(assetStore.DownloadAsync(fileName, stream, range, ct));
        }
Exemplo n.º 13
0
        public async Task Should_download_file_from_store()
        {
            var stream = new MemoryStream();

            await sut.DownloadAsync(appId, assetId, assetFileVersion, stream);

            A.CallTo(() => assetStore.DownloadAsync(fileNameNew, stream, default, CancellationToken.None))
Exemplo n.º 14
0
        public IActionResult GetImage(string app)
        {
            if (App.Image == null)
            {
                return(NotFound());
            }

            var etag = App.Image.Etag;

            Response.Headers[HeaderNames.ETag] = etag;

            var callback = new FileCallback(async(body, range, ct) =>
            {
                var resizedAsset = $"{App.Id}_{etag}_Resized";

                try
                {
                    await assetStore.DownloadAsync(resizedAsset, body, ct: ct);
                }
                catch (AssetNotFoundException)
                {
                    await ResizeAsync(resizedAsset, App.Image.MimeType, body, ct);
                }
            });

            return(new FileCallbackResult(App.Image.MimeType, callback)
            {
                ErrorAs404 = true
            });
        }
Exemplo n.º 15
0
        public static async Task DownloadAsync(this IAssetStore assetStore, DirectoryInfo directory)
        {
            if (directory.Exists)
            {
                directory.Delete(true);
            }

            directory.Create();

            using (var fileStream = new FileStream(
                       Path.Combine(directory.FullName, ArchiveFile),
                       FileMode.Create,
                       FileAccess.ReadWrite,
                       FileShare.None,
                       4096,
                       FileOptions.DeleteOnClose))
            {
                try
                {
                    await assetStore.DownloadAsync(directory.Name, 0, string.Empty, fileStream);

                    fileStream.Position = 0;

                    using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read, true))
                    {
                        zipArchive.ExtractToDirectory(directory.FullName);
                    }
                }
                catch (AssetNotFoundException)
                {
                    return;
                }
            }
        }
Exemplo n.º 16
0
        public async Task Should_invoke_asset_store_to_download_picture()
        {
            A.CallTo(() => assetStore.DownloadAsync(userId, 0, "picture", A <Stream> .Ignored, CancellationToken.None))
            .Invokes(async(IFakeObjectCall call) =>
            {
                var stream = call.GetArgument <Stream>(3);

                await stream.WriteAsync(new byte[] { 1, 2, 3, 4 }, 0, 4);
            });

            var result = await sut.DownloadAsync(userId);

            Assert.Equal(0, result.Position);
            Assert.Equal(4, result.Length);

            A.CallTo(() => assetStore.DownloadAsync(userId, 0, "picture", A <Stream> .Ignored, CancellationToken.None)).MustHaveHappened();
        }
        public async Task Should_invoke_asset_store_to_download_archive_using_suffix_for_compatibility()
        {
            var stream = new MemoryStream();

            await sut.DownloadAsync(backupId, stream);

            A.CallTo(() => assetStore.DownloadAsync(fileName, stream, CancellationToken.None))
            .MustHaveHappened();
        }
Exemplo n.º 18
0
        public async Task Should_invoke_asset_store_to_download_file()
        {
            var stream = new MemoryStream();

            await sut.DownloadAsync(assetId, assetFileVersion, stream);

            A.CallTo(() => assetStore.DownloadAsync(fileName, stream, CancellationToken.None))
            .MustHaveHappened();
        }
Exemplo n.º 19
0
        public IActionResult GetImage(string app)
        {
            if (App.Image == null)
            {
                return(NotFound());
            }

            var etag = App.Image.Etag;

            Response.Headers[HeaderNames.ETag] = etag;

            var callback = new FileCallback(async(body, range, ct) =>
            {
                var resizedAsset = $"{App.Id}_{etag}_Resized";

                try
                {
                    await assetStore.DownloadAsync(resizedAsset, body, ct: ct);
                }
                catch (AssetNotFoundException)
                {
                    using (Profiler.Trace("Resize"))
                    {
                        using (var sourceStream = GetTempStream())
                        {
                            using (var destinationStream = GetTempStream())
                            {
                                using (Profiler.Trace("ResizeDownload"))
                                {
                                    await appImageStore.DownloadAsync(App.Id, sourceStream);
                                    sourceStream.Position = 0;
                                }

                                using (Profiler.Trace("ResizeImage"))
                                {
                                    await assetThumbnailGenerator.CreateThumbnailAsync(sourceStream, destinationStream, ResizeOptions);
                                    destinationStream.Position = 0;
                                }

                                using (Profiler.Trace("ResizeUpload"))
                                {
                                    await assetStore.UploadAsync(resizedAsset, destinationStream);
                                    destinationStream.Position = 0;
                                }

                                await destinationStream.CopyToAsync(body, ct);
                            }
                        }
                    }
                }
            });

            return(new FileCallbackResult(App.Image.MimeType, callback)
            {
                ErrorAs404 = true
            });
        }
        public async Task <Stream> DownloadAsync(string userId)
        {
            var memoryStream = new MemoryStream();

            await assetStore.DownloadAsync(userId, 0, "picture", memoryStream);

            memoryStream.Position = 0;

            return(memoryStream);
        }
Exemplo n.º 21
0
        public async Task <IActionResult> GetAssetContent(string app, Guid id, [FromQuery] int version = -1, [FromQuery] int?width = null, [FromQuery] int?height = null, [FromQuery] string mode = null)
        {
            var asset = await assetRepository.FindAssetAsync(id);

            if (asset == null || asset.FileVersion < version || width == 0 || height == 0)
            {
                return(NotFound());
            }

            return(new FileCallbackResult(asset.MimeType, asset.FileName, async bodyStream =>
            {
                if (asset.IsImage && (width.HasValue || height.HasValue))
                {
                    var suffix = $"{width}_{height}_{mode}";

                    try
                    {
                        await assetStorage.DownloadAsync(asset.Id, asset.FileVersion, suffix, bodyStream);
                    }
                    catch (AssetNotFoundException)
                    {
                        using (var sourceStream = GetTempStream())
                        {
                            using (var destinationStream = GetTempStream())
                            {
                                await assetStorage.DownloadAsync(asset.Id, asset.FileVersion, null, sourceStream);
                                sourceStream.Position = 0;

                                await assetThumbnailGenerator.CreateThumbnailAsync(sourceStream, destinationStream, width, height, mode);
                                destinationStream.Position = 0;

                                await assetStorage.UploadAsync(asset.Id, asset.FileVersion, suffix, destinationStream);
                                destinationStream.Position = 0;

                                await destinationStream.CopyToAsync(bodyStream);
                            }
                        }
                    }
                }

                await assetStorage.DownloadAsync(asset.Id, asset.FileVersion, null, bodyStream);
            }));
        }
Exemplo n.º 22
0
        public async Task Should_download_file_from_store(bool folderPerApp, string?suffix, string fileName)
        {
            var fullName = GetFullName(fileName);

            options.FolderPerApp = folderPerApp;

            var stream = new MemoryStream();

            await sut.DownloadAsync(appId, assetId, assetFileVersion, suffix, stream);

            A.CallTo(() => assetStore.DownloadAsync(fullName, stream, default, CancellationToken.None))
        public async Task Should_invoke_asset_store_to_download_archive(bool folderPerApp)
        {
            var stream = new MemoryStream();

            options.FolderPerApp = folderPerApp;

            var fileName = GetFileName(folderPerApp);

            await sut.DownloadAsync(appId, stream);

            A.CallTo(() => assetStore.DownloadAsync(fileName, stream, default, CancellationToken.None))
        public async Task <IActionResult> GetBackupContent(string app, Guid id)
        {
            var backupGrain = grainFactory.GetGrain <IBackupGrain>(AppId);

            var backups = await backupGrain.GetStateAsync();

            var backup = backups.Value.Find(x => x.Id == id);

            if (backup == null || backup.Status != JobStatus.Completed)
            {
                return(NotFound());
            }

            var fileName = $"backup-{app}-{backup.Started:yyyy-MM-dd_HH-mm-ss}.zip";

            return(new FileCallbackResult("application/zip", fileName, false, bodyStream =>
            {
                return assetStore.DownloadAsync(id.ToString(), 0, null, bodyStream);
            }));
        }
Exemplo n.º 25
0
        protected override async Task OnActivateAsync(Guid key)
        {
            directory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), $"Index_{key}"));

            await assetStore.DownloadAsync(directory);

            var config = new IndexWriterConfig(Version, Analyzer)
            {
                IndexDeletionPolicy = snapshotter
            };

            indexWriter = new IndexWriter(FSDirectory.Open(directory), config);

            if (indexWriter.NumDocs > 0)
            {
                OpenReader();
            }
            else
            {
                indexState = new IndexState(indexReader, indexWriter);
            }
        }
Exemplo n.º 26
0
        public Task DownloadAsync(Guid id, long fileVersion, Stream stream, BytesRange range = default, CancellationToken ct = default)
        {
            var fileName = GetFileName(id, fileVersion);

            return(assetStore.DownloadAsync(fileName, stream, range, ct));
        }
Exemplo n.º 27
0
        public Task DownloadAsync(Guid backupId, Stream stream, CancellationToken ct = default)
        {
            var fileName = GetFileName(backupId);

            return(assetStore.DownloadAsync(fileName, stream, default, ct));
Exemplo n.º 28
0
        private IActionResult DeliverAsset(string appId, MediaItem?media, MediaFileQueryDto?query)
        {
            query ??= new MediaFileQueryDto();

            if (media == null)
            {
                return(NotFound());
            }

            var resizeOptions = query.ToResizeOptions();

            FileCallback callback;

            if (query.CacheDuration > 0)
            {
                Response.Headers[HeaderNames.CacheControl] = $"public,max-age={query.CacheDuration}";
            }

            var contentLength = (long?)null;

            if (media.Type == MediaType.Image && resizeOptions.IsValid)
            {
                callback = async(bodyStream, range, ct) =>
                {
                    var resizedAsset = $"{appId}_{media.FileName}_{resizeOptions}";

                    if (query.ForceResize)
                    {
                        await ResizeAsync(appId, media, bodyStream, resizedAsset, resizeOptions, true, ct);
                    }
                    else
                    {
                        try
                        {
                            await assetStore.DownloadAsync(resizedAsset, bodyStream, ct : ct);
                        }
                        catch (AssetNotFoundException)
                        {
                            await ResizeAsync(appId, media, bodyStream, resizedAsset, resizeOptions, false, ct);
                        }
                    }
                };
            }
            else
            {
                contentLength = media.FileSize;

                callback = async(bodyStream, range, ct) =>
                {
                    await mediaFileStore.DownloadAsync(appId, media, bodyStream, range, ct);
                };
            }

            return(new FileCallbackResult(media.MimeType, callback)
            {
                EnableRangeProcessing = contentLength > 0,
                ErrorAs404 = true,
                FileDownloadName = media.FileName,
                FileSize = contentLength,
                SendInline = query.Download != 1
            });
        }
Exemplo n.º 29
0
        private IActionResult DeliverAsset(IAssetEntity?asset, AssetQuery query)
        {
            query ??= new AssetQuery();

            if (asset == null || asset.FileVersion < query.Version)
            {
                return(NotFound());
            }

            if (asset.IsProtected && !this.HasPermission(Permissions.AppAssetsRead))
            {
                return(StatusCode(403));
            }

            var fileVersion = query.Version;

            if (fileVersion <= EtagVersion.Any)
            {
                fileVersion = asset.FileVersion;
            }

            Response.Headers[HeaderNames.ETag] = fileVersion.ToString();

            if (query.CacheDuration > 0)
            {
                Response.Headers[HeaderNames.CacheControl] = $"public,max-age={query.CacheDuration}";
            }

            var handler = new Func <Stream, Task>(async bodyStream =>
            {
                if (asset.Type == AssetType.Image && query.ShouldResize())
                {
                    var resizedAsset = $"{asset.Id}_{asset.FileVersion}_{query.Width}_{query.Height}_{query.Mode}";

                    if (query.Quality.HasValue)
                    {
                        resizedAsset += $"_{query.Quality}";
                    }

                    try
                    {
                        await assetStore.DownloadAsync(resizedAsset, bodyStream);
                    }
                    catch (AssetNotFoundException)
                    {
                        using (Profiler.Trace("Resize"))
                        {
                            using (var sourceStream = GetTempStream())
                            {
                                using (var destinationStream = GetTempStream())
                                {
                                    using (Profiler.Trace("ResizeDownload"))
                                    {
                                        await assetFileStore.DownloadAsync(asset.Id, fileVersion, sourceStream);
                                        sourceStream.Position = 0;
                                    }

                                    using (Profiler.Trace("ResizeImage"))
                                    {
                                        await assetThumbnailGenerator.CreateThumbnailAsync(sourceStream, destinationStream, query.Width, query.Height, query.Mode, query.Quality);
                                        destinationStream.Position = 0;
                                    }

                                    using (Profiler.Trace("ResizeUpload"))
                                    {
                                        await assetStore.UploadAsync(resizedAsset, destinationStream);
                                        destinationStream.Position = 0;
                                    }

                                    await destinationStream.CopyToAsync(bodyStream);
                                }
                            }
                        }
                    }
                }
                else
                {
                    await assetFileStore.DownloadAsync(asset.Id, fileVersion, bodyStream);
                }
            });

            if (query.Download == 1)
            {
                return(new FileCallbackResult(asset.MimeType, asset.FileName, true, handler));
            }
            else
            {
                return(new FileCallbackResult(asset.MimeType, null, true, handler));
            }
        }
Exemplo n.º 30
0
        public async Task RunAsync()
        {
            if (currentTask != null)
            {
                throw new DomainException("Another backup process is already running.");
            }

            if (state.Jobs.Count >= MaxBackups)
            {
                throw new DomainException($"You cannot have more than {MaxBackups} backups.");
            }

            var job = new BackupStateJob {
                Id = Guid.NewGuid(), Started = clock.GetCurrentInstant()
            };

            currentTask = new CancellationTokenSource();
            currentJob  = job;

            var lastTimestamp = job.Started;

            state.Jobs.Insert(0, job);

            await WriteAsync();

            try
            {
                using (var stream = await backupArchiveLocation.OpenStreamAsync(job.Id))
                {
                    using (var writer = new EventStreamWriter(stream))
                    {
                        await eventStore.QueryAsync(async @event =>
                        {
                            var eventData = @event.Data;

                            if (eventData.Type == "AssetCreatedEvent" ||
                                eventData.Type == "AssetUpdatedEvent")
                            {
                                var parsedEvent = eventDataFormatter.Parse(eventData);

                                var assetVersion = 0L;
                                var assetId      = Guid.Empty;

                                if (parsedEvent.Payload is AssetCreated assetCreated)
                                {
                                    assetId      = assetCreated.AssetId;
                                    assetVersion = assetCreated.FileVersion;
                                }

                                if (parsedEvent.Payload is AssetUpdated asetUpdated)
                                {
                                    assetId      = asetUpdated.AssetId;
                                    assetVersion = asetUpdated.FileVersion;
                                }

                                await writer.WriteEventAsync(eventData, async attachmentStream =>
                                {
                                    await assetStore.DownloadAsync(assetId.ToString(), assetVersion, null, attachmentStream);
                                });

                                job.HandledAssets++;
                            }
                            else
                            {
                                await writer.WriteEventAsync(eventData);
                            }

                            job.HandledEvents++;

                            var now = clock.GetCurrentInstant();

                            if ((now - lastTimestamp) >= UpdateDuration)
                            {
                                lastTimestamp = now;

                                await WriteAsync();
                            }
                        }, SquidexHeaders.AppId, appId.ToString(), null, currentTask.Token);
                    }

                    stream.Position = 0;

                    currentTask.Token.ThrowIfCancellationRequested();

                    await assetStore.UploadAsync(job.Id.ToString(), 0, null, stream, currentTask.Token);
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "makeBackup")
                             .WriteProperty("status", "failed")
                             .WriteProperty("backupId", job.Id.ToString()));

                job.IsFailed = true;
            }
            finally
            {
                await CleanupArchiveAsync(job);

                job.Stopped = clock.GetCurrentInstant();

                await WriteAsync();

                currentTask = null;
                currentJob  = null;
            }
        }