Ejemplo n.º 1
0
        public async Task <ActionResult> StopSharingFileWithUser(ShareFileRequest request)
        {
            var userId = AuthenticationManager.GetUserId(User);

            if (request.UserId == userId.ToString())
            {
                return(BadRequest("You can't stop sharing file with yourself"));
            }

            var file = await _filesRepository.FindByIdAsync(request.FileId);

            if (!file.AllowedUsers.Contains(new ObjectId(request.UserId)))
            {
                return(BadRequest("File is not shared with this user"));
            }

            var updatedAllowedList = file.AllowedUsers;

            updatedAllowedList.Remove(new ObjectId(request.UserId));

            if (!FileFolderManager.CanDeleteFile(userId, file))
            {
                return(BadRequest("You don't have rights to stop sharing this file"));
            }

            await _filesRepository.UpdateOneAsync(request.FileId, "AllowedUsers", updatedAllowedList);

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ShareFileWithUser(ShareFileRequest request)
        {
            var userId = AuthenticationManager.GetUserId(User);

            if (request.UserId == userId.ToString())
            {
                return(BadRequest("You already have access to this file"));
            }

            var file = await _filesRepository.FindByIdAsync(request.FileId);

            if (file.AllowedUsers.Contains(new ObjectId(request.UserId)))
            {
                return(BadRequest("User already has access to this file"));
            }

            var updatedAllowedList = file.AllowedUsers;

            updatedAllowedList.Add(new ObjectId(request.UserId));

            if (!FileFolderManager.CanDeleteFile(userId, file))
            {
                return(BadRequest("You don't have rights to share this file"));
            }

            await _filesRepository.UpdateOneAsync(request.FileId, "AllowedUsers", updatedAllowedList);

            return(Ok());
        }
        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());
        }
        public async Task <ActionResult> SwitchFavouriteFile(FavouriteSwitch favouriteSwitch)
        {
            var userId = Tools.AuthenticationManager.GetUserId(User);
            var file   = await _filesRepository.FindByIdAsync(favouriteSwitch.Id);

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

            await _filesRepository.UpdateOneAsync(favouriteSwitch.Id.ToString(), "Favourite", favouriteSwitch.IsFavourite);

            file.Favourite = favouriteSwitch.IsFavourite;

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