public async Task <ActionResult> DeleteFileAsync(ObjectIdRequest request)
        {
            var userId = Tools.AuthenticationManager.GetUserId(User);

            #region Get file from db
            DatabaseModule.Entities.File file;
            try
            {
                file = await _filesRepository.FindByIdAsync(request.Id);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(BadRequest("File not found"));
            }
            #endregion

            if (!FileFolderManager.CanDeleteFile(userId, file))
            {
                return(BadRequest("You can't delete file or it doesn't exist"));
            }

            #region Get bucket from db
            DatabaseModule.Entities.Bucket bucket;
            try
            {
                bucket = await _bucketsRepository.FindByIdAsync(file.BucketId.ToString());
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(BadRequest("Bucket not found"));
            }
            #endregion


            var serviceConfig        = bucket.BucketConfigData;
            var googleBucketUploader = new RequestHandlerGoogleBucket(serviceConfig.ConfigData, serviceConfig.SelectedBucket);
            var result = googleBucketUploader.DeleteFile(request.Id);

            if (!result)
            {
                return(BadRequest("Error while deleting your file"));
            }

            #region Delete file from db
            try
            {
                await _filesRepository.DeleteByIdAsync(request.Id);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(BadRequest("Deletion file from db failed"));
            }
            #endregion

            return(Ok());
        }
Exemple #2
0
        public async Task <ActionResult <String> > UserInfo(ObjectIdRequest request)
        {
            var user = await _usersRepository.FindByIdAsync(request.Id);

            var userInfo = new UserInfo(user);

            return(Ok(JsonSerializer.Serialize(userInfo)));
        }
        public async Task <ActionResult> GetFileInfo(ObjectIdRequest fileIdRequest)
        {
            var userId = Tools.AuthenticationManager.GetUserId(User);
            var file   = await _filesRepository.FindByIdAsync(fileIdRequest.Id);

            if (!FileFolderManager.CanAccessFile(userId, file))
            {
                return(BadRequest("You don't have access to file or it doesn't exist"));
            }

            return(Ok(JsonSerializer.Serialize(
                          new FileInfoWithUser(file),
                          new JsonSerializerOptions()
            {
                Converters =
                {
                    new FileInfoWithUserSerializer()
                }
            })));
        }