Example #1
0
        public async Task <IActionResult> Post(IFormFile file)
        {
            try
            {
                if (file == null || file.Length == 0)
                {
                    return(BadRequest("No file was provided"));
                }

                var fileUploadResult = await _fileUploader.UploadAsync(file);

                var fileEntity = CreateFile(file, fileUploadResult);

                var response = new UploadResponseViewModel
                {
                    Success  = true,
                    FileName = fileEntity.FileName,
                    Uri      = fileEntity.ExternalUri
                };

                response.Id = await _repo.AddFileAsync(fileEntity);

                return(Ok(response));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(StatusCode(500, "An error occurred while trying to upload the file"));
            }
        }
        public async Task <string> UploadAsync(Guid postId, IFormFile imageFile, CancellationToken cancellationToken)
        {
            var fileName = NormalizeFileName($"{Guid.NewGuid()}_{imageFile.FileName}");

            using (var fileStream = imageFile.OpenReadStream())
            {
                fileName = await fileUploader.UploadAsync(postId.ToString(), fileName, imageFile.ContentType, fileStream, cancellationToken);
            }

            return(fileName);
        }
        public async Task <string> Handle(UpdateCurrentUserPhotoCommand request, CancellationToken cancellationToken)
        {
            var profile = await _identityService.GetCurrentUserProfileAsync();

            var file = request.File;

            profile.ProfilePicture = await _fileUploader.UploadAsync(file);

            await _identityService.UpdateCurrentUserProfileAsync(profile);

            return(profile.ProfilePicture);
        }
        public async Task RegisterNewProductAsync(Domain.Entities.Product product, List <RequestFile> photos, RequestFile profile)
        {
            const string profilePhotoName = nameof(product.ProfilePhoto);
            const string photosName       = nameof(product.Photos);
            const string bucketName       = nameof(profile.Bucket);
            const string nameOfPhotosName = nameof(RequestFile.Name);

            var productValidation = product.Validate(profilePhotoName, photosName);
            var profileValidation = profile.Validate(bucketName, nameOfPhotosName);
            var photosValidation  = photos?.SelectMany(p => p.Validate(bucketName, nameOfPhotosName));

            if (productValidation.IsValid() && profileValidation.IsValid() && photosValidation.IsValid())
            {
                await _repository.CreateProductAsync(product);

                _uploader.FileUploaded += OnPhotoUploaded;

                profile.Name     = $"{ProductName}-{product.Key}-{ProfileTypeName}-{profile.Key}".ToLower();
                profile.Bucket   = "store-lab-product-profile";
                profile.Metadata = new Dictionary <string, string>
                {
                    { ProductKeyName, product.Key },
                    { FileTypeName, ProfileTypeName }
                };

                photos?.ForEach(p =>
                {
                    p.Name     = $"{ProductName}-{product.Key}-{PhotoTypeName}-{p.Key}".ToLower();
                    p.Bucket   = "store-lab-product-photos";
                    p.Metadata = new Dictionary <string, string>
                    {
                        { ProductKeyName, product.Key },
                        { FileTypeName, PhotoTypeName }
                    };
                });

                await _uploader.UploadAsync(profile);

                await _uploader.UploadAllAsync(photos);

                var args = new RegisterNewProductEventArgs {
                    Product = product
                };

                ProductRegisted?.Invoke(this, args);
            }
            else
            {
                throw new EntityException(productValidation.Aggregate(profileValidation).Aggregate(photosValidation));
            }
        }
Example #5
0
        public async Task <CommandHandlingResult> Handle(ExportClientHistoryCommand command, IEventPublisher publisher)
        {
            var result = new List <BaseHistoryModel>();

            for (var i = 0; ; i++)
            {
                var response = await _historyClient.HistoryApi.GetHistoryByWalletAsync(Guid.Parse(command.ClientId),
                                                                                       command.OperationTypes,
                                                                                       command.AssetId,
                                                                                       command.AssetPairId,
                                                                                       i *PageSize,
                                                                                       PageSize);

                if (!response.Any())
                {
                    break;
                }

                result.AddRange(response);
            }

            var history = result.Select(x => x.ToHistoryModel()).OrderByDescending(x => x.DateTime);

            var idForUri = await _fileMapper.MapAsync(command.ClientId, command.Id);

            var file = await _fileMaker.MakeAsync(history);

            var uri = await _fileUploader.UploadAsync(idForUri, FileType.Csv, file);

            await _expiryWatcher.AddAsync(
                new ExpiryEntry
            {
                ClientId       = command.ClientId,
                RequestId      = command.Id,
                ExpiryDateTime = DateTime.UtcNow + _ttl
            });

            publisher.PublishEvent(new ClientHistoryExportedEvent
            {
                Id       = command.Id,
                ClientId = command.ClientId,
                Uri      = uri
            });

            return(CommandHandlingResult.Ok());
        }
Example #6
0
 public Task UploadAsync(FileUploadSpec spec) => Wrap(() => _uploader.UploadAsync(spec), spec);
Example #7
0
        /// <summary>
        /// When overridden in the derived class, handles synchronizing the given version.
        /// </summary>
        /// <param name="fu">The <see cref="IFileUploader"/> to use.</param>
        /// <param name="v">The version to synchronize.</param>
        /// <param name="reEnqueue">True if the <paramref name="v"/> should be re-enqueued so it can be re-attempted.
        /// If the method throws an <see cref="Exception"/>, the <paramref name="v"/> will be re-enqueued no matter what.</param>
        /// <returns>
        /// The error string, or null if the synchronization was successful.
        /// </returns>
        protected override string DoSync(IFileUploader fu, int v, out bool reEnqueue)
        {
            reEnqueue = false;

            // Load the VersionFileList for the version to check
            var vflPath = VersionHelper.GetVersionFileListPath(v);
            if (!File.Exists(vflPath))
            {
                // Version doesn't exist at all
                return null;
            }

            var vfl = VersionFileList.CreateFromFile(vflPath);

            // Try to download the version's file list hash
            var fileListHashPath = GetVersionRemoteFilePath(v, PathHelper.RemoteFileListHashFileName);
            var vflHash = fu.DownloadAsString(fileListHashPath);

            // Check if the hash file exists on the server
            if (vflHash != null)
            {
                // Check if the hash matches the current version's hash
                var expectedVflHash = File.ReadAllText(VersionHelper.GetVersionFileListHashPath(v));
                if (vflHash != expectedVflHash)
                {
                    // Delete the whole version folder first
                    fu.DeleteDirectory(GetVersionRemoteFilePath(v, null));
                }
                else
                {
                    // Hash existed and was correct - good enough for us!
                    return null;
                }
            }
            else
            {
                // Hash didn't exist at all, so we will have to update. As long as SkipIfExists is set to true, files
                // that already exist will be skipped, so we will only end up uploading the new files. In any case, its
                // the same process either way.
            }

            // Check the hashes of the local files
            foreach (var f in vfl.Files)
            {
                // Get the local file path
                var localPath = VersionHelper.GetVersionFile(v, f.FilePath);

                // Confirm the hash of the file
                var fileHash = Hasher.GetFileHash(localPath);
                if (fileHash != f.Hash)
                {
                    const string errmsg =
                        "The cached hash ({0}) of file `{1}` does not match the real hash ({2}) for version {3}." +
                        " Possible version corruption.";
                    return string.Format(errmsg, f.Hash, f.FilePath, fileHash, v);
                }
            }

            // Hashes check out, start uploading
            foreach (var f in vfl.Files)
            {
                // Get the local file path
                var localPath = VersionHelper.GetVersionFile(v, f.FilePath);

                var remotePath = GetVersionRemoteFilePath(v, f.FilePath);
                fu.UploadAsync(localPath, remotePath);
            }

            // Wait for uploads to finish
            while (fu.IsBusy)
            {
                Thread.Sleep(1000);
            }

            // All uploads have finished, so upload the VersionFileList hash
            fu.UploadAsync(VersionHelper.GetVersionFileListHashPath(v), fileListHashPath);

            // All done! That was easy enough, eh? *sigh*
            return null;
        }
Example #8
0
        /// <summary>
        /// When overridden in the derived class, handles synchronizing the given version.
        /// </summary>
        /// <param name="fu">The <see cref="IFileUploader"/> to use.</param>
        /// <param name="v">The version to synchronize.</param>
        /// <param name="reEnqueue">True if the <paramref name="v"/> should be re-enqueued so it can be re-attempted.
        /// If the method throws an <see cref="Exception"/>, the <paramref name="v"/> will be re-enqueued no matter what.</param>
        /// <returns>The error string, or null if the synchronization was successful.</returns>
        protected override string DoSync(IFileUploader fu, int v, out bool reEnqueue)
        {
            fu.SkipIfExists = false;

            reEnqueue = false;

            var remoteFileListFilePath     = PathHelper.GetVersionString(v) + ".txt";
            var remoteFileListHashFilePath = PathHelper.GetVersionString(v) + ".hash";

            // Ensure the live version is written. This is a very small but very important file, so just write it during
            // every synchronization.
            fu.UploadAsync(_settings.LiveVersionFilePath, MasterServerReader.CurrentVersionFilePath);

            // Also ensure the master server and file server lists are up-to-date. Again, we will just do this every time we
            // check to sync since they are relatively small lists but very important to keep up-to-date.
            fu.UploadAsync(_settings.FileServerListFilePath, MasterServerReader.CurrentDownloadSourcesFilePath);
            fu.UploadAsync(_settings.MasterServerListFilePath, MasterServerReader.CurrentMasterServersFilePath);

            // Load the VersionFileList for the version to check
            var vflPath = VersionHelper.GetVersionFileListPath(v);

            if (!File.Exists(vflPath))
            {
                // Version doesn't exist at all
                while (fu.IsBusy)
                {
                    Thread.Sleep(500);
                }
                return(null);
            }

            // Test the creation of the VersionFileList to ensure its valid
            VersionFileList.CreateFromFile(vflPath);

            // Try to download the version's file list hash
            var vflHash = fu.DownloadAsString(remoteFileListHashFilePath);

            // Check if the hash file exists on the server
            if (vflHash != null)
            {
                // Check if the hash matches the current version's hash
                var expectedVflHash = File.ReadAllText(VersionHelper.GetVersionFileListHashPath(v));
                if (vflHash != expectedVflHash)
                {
                    // We don't need to delete anything since we make the MasterServer overwrite instead
                }
                else
                {
                    // Hash existed and was correct - good enough for us! But first, wait for uploads to finish...
                    while (fu.IsBusy)
                    {
                        Thread.Sleep(500);
                    }

                    return(null);
                }
            }
            else
            {
                // Hash didn't exist at all, so we will have to update. As long as SkipIfExists is set to true, files
                // that already exist will be skipped, so we will only end up uploading the new files. In any case, its
                // the same process either way.
            }

            // Upload the files
            fu.UploadAsync(VersionHelper.GetVersionFileListPath(v), remoteFileListFilePath);
            fu.UploadAsync(VersionHelper.GetVersionFileListHashPath(v), remoteFileListHashFilePath);

            // Wait for uploads to finish
            while (fu.IsBusy)
            {
                Thread.Sleep(1000);
            }

            // All done!
            return(null);
        }
Example #9
0
        /// <summary>
        /// When overridden in the derived class, handles synchronizing the given version.
        /// </summary>
        /// <param name="fu">The <see cref="IFileUploader"/> to use.</param>
        /// <param name="v">The version to synchronize.</param>
        /// <param name="reEnqueue">True if the <paramref name="v"/> should be re-enqueued so it can be re-attempted.
        /// If the method throws an <see cref="Exception"/>, the <paramref name="v"/> will be re-enqueued no matter what.</param>
        /// <returns>
        /// The error string, or null if the synchronization was successful.
        /// </returns>
        protected override string DoSync(IFileUploader fu, int v, out bool reEnqueue)
        {
            reEnqueue = false;

            // Load the VersionFileList for the version to check
            var vflPath = VersionHelper.GetVersionFileListPath(v);

            if (!File.Exists(vflPath))
            {
                // Version doesn't exist at all
                return(null);
            }

            var vfl = VersionFileList.CreateFromFile(vflPath);

            // Try to download the version's file list hash
            var fileListHashPath = GetVersionRemoteFilePath(v, PathHelper.RemoteFileListHashFileName);
            var vflHash          = fu.DownloadAsString(fileListHashPath);

            // Check if the hash file exists on the server
            if (vflHash != null)
            {
                // Check if the hash matches the current version's hash
                var expectedVflHash = File.ReadAllText(VersionHelper.GetVersionFileListHashPath(v));
                if (vflHash != expectedVflHash)
                {
                    // Delete the whole version folder first
                    fu.DeleteDirectory(GetVersionRemoteFilePath(v, null));
                }
                else
                {
                    // Hash existed and was correct - good enough for us!
                    return(null);
                }
            }
            else
            {
                // Hash didn't exist at all, so we will have to update. As long as SkipIfExists is set to true, files
                // that already exist will be skipped, so we will only end up uploading the new files. In any case, its
                // the same process either way.
            }

            // Check the hashes of the local files
            foreach (var f in vfl.Files)
            {
                // Get the local file path
                var localPath = VersionHelper.GetVersionFile(v, f.FilePath);

                // Confirm the hash of the file
                var fileHash = Hasher.GetFileHash(localPath);
                if (fileHash != f.Hash)
                {
                    const string errmsg =
                        "The cached hash ({0}) of file `{1}` does not match the real hash ({2}) for version {3}." +
                        " Possible version corruption.";
                    return(string.Format(errmsg, f.Hash, f.FilePath, fileHash, v));
                }
            }

            // Hashes check out, start uploading
            foreach (var f in vfl.Files)
            {
                // Get the local file path
                var localPath = VersionHelper.GetVersionFile(v, f.FilePath);

                var remotePath = GetVersionRemoteFilePath(v, f.FilePath);
                fu.UploadAsync(localPath, remotePath);
            }

            // Wait for uploads to finish
            while (fu.IsBusy)
            {
                Thread.Sleep(1000);
            }

            // All uploads have finished, so upload the VersionFileList hash
            fu.UploadAsync(VersionHelper.GetVersionFileListHashPath(v), fileListHashPath);

            // All done! That was easy enough, eh? *sigh*
            return(null);
        }
Example #10
0
        /// <summary>
        /// When overridden in the derived class, handles synchronizing the given version.
        /// </summary>
        /// <param name="fu">The <see cref="IFileUploader"/> to use.</param>
        /// <param name="v">The version to synchronize.</param>
        /// <param name="reEnqueue">True if the <paramref name="v"/> should be re-enqueued so it can be re-attempted.
        /// If the method throws an <see cref="Exception"/>, the <paramref name="v"/> will be re-enqueued no matter what.</param>
        /// <returns>The error string, or null if the synchronization was successful.</returns>
        protected override string DoSync(IFileUploader fu, int v, out bool reEnqueue)
        {
            fu.SkipIfExists = false;

            reEnqueue = false;

            var remoteFileListFilePath = PathHelper.GetVersionString(v) + ".txt";
            var remoteFileListHashFilePath = PathHelper.GetVersionString(v) + ".hash";

            // Ensure the live version is written. This is a very small but very important file, so just write it during
            // every synchronization.
            fu.UploadAsync(_settings.LiveVersionFilePath, MasterServerReader.CurrentVersionFilePath);

            // Also ensure the master server and file server lists are up-to-date. Again, we will just do this every time we
            // check to sync since they are relatively small lists but very important to keep up-to-date.
            fu.UploadAsync(_settings.FileServerListFilePath, MasterServerReader.CurrentDownloadSourcesFilePath);
            fu.UploadAsync(_settings.MasterServerListFilePath, MasterServerReader.CurrentMasterServersFilePath);

            // Load the VersionFileList for the version to check
            var vflPath = VersionHelper.GetVersionFileListPath(v);
            if (!File.Exists(vflPath))
            {
                // Version doesn't exist at all
                while (fu.IsBusy)
                {
                    Thread.Sleep(500);
                }
                return null;
            }

            // Test the creation of the VersionFileList to ensure its valid
            VersionFileList.CreateFromFile(vflPath);

            // Try to download the version's file list hash
            var vflHash = fu.DownloadAsString(remoteFileListHashFilePath);

            // Check if the hash file exists on the server
            if (vflHash != null)
            {
                // Check if the hash matches the current version's hash
                var expectedVflHash = File.ReadAllText(VersionHelper.GetVersionFileListHashPath(v));
                if (vflHash != expectedVflHash)
                {
                    // We don't need to delete anything since we make the MasterServer overwrite instead
                }
                else
                {
                    // Hash existed and was correct - good enough for us! But first, wait for uploads to finish...
                    while (fu.IsBusy)
                    {
                        Thread.Sleep(500);
                    }

                    return null;
                }
            }
            else
            {
                // Hash didn't exist at all, so we will have to update. As long as SkipIfExists is set to true, files
                // that already exist will be skipped, so we will only end up uploading the new files. In any case, its
                // the same process either way.
            }

            // Upload the files
            fu.UploadAsync(VersionHelper.GetVersionFileListPath(v), remoteFileListFilePath);
            fu.UploadAsync(VersionHelper.GetVersionFileListHashPath(v), remoteFileListHashFilePath);

            // Wait for uploads to finish
            while (fu.IsBusy)
            {
                Thread.Sleep(1000);
            }

            // All done!
            return null;
        }
 public static Task UploadAsync(this IFileUploader uploader, IAbsoluteFilePath localFile, Uri uri,
                                ITransferProgress progress) => uploader.UploadAsync(new FileUploadSpec(localFile, uri, progress));
 public static Task UploadAsync(this IFileUploader uploader, IAbsoluteFilePath localFile, Uri uri)
 => uploader.UploadAsync(new FileUploadSpec(localFile, uri));