コード例 #1
0
        public byte[] DownloadFile(File fileInfo)
        {
            var newObject = new Google.Apis.Storage.v1.Data.Object()
            {
                Bucket = BucketToUpload,
                Name   = fileInfo.Id.ToString()
            };

            Stream memoryStream = new MemoryStream();

            byte[] result = null;
            try
            {
                var downloadRequest = new ObjectsResource.GetRequest(StorageService,
                                                                     BucketToUpload, fileInfo.Id.ToString());
                var resultStatus = downloadRequest.DownloadWithStatus(memoryStream);
                result = ReadToEnd(memoryStream);

                #region Decryption and decompressing
                if (fileInfo.Encrypted)
                {
                    result = Decrypt(result, Convert.FromBase64String(fileInfo.EncryptionKey), Convert.FromBase64String(fileInfo.IV)).Take((int)fileInfo.FileSize).ToArray();
                }
                if (fileInfo.Compressed)
                {
                    result = Decompress(result);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                memoryStream.Dispose();
            }
            return(result);
        }
        public async Task <ActionResult <string> > SendFileAsync(IFormFile fileUpload, bool compressed, bool encrypted, bool favourite, string folderId)
        {
            var formData = HttpContext.Request.Form;
            var files    = HttpContext.Request.Form.Files;

            var file = files.First();

            ObjectId _folderId = new ObjectId(folderId);
            bool     available = await FileFolderManager.CanAccessFolder(Tools.AuthenticationManager.GetUserId(User), _folderId, _foldersRepository);

            if (!available)
            {
                return(BadRequest("Folder not available or doesn't exist"));
            }


            var user = await _usersRepository.FindOneAsync(x => x.Id == new ObjectId(User.FindFirst("id").Value));

            if (user.Buckets.Count() == 0)
            {
                return(BadRequest("You have not linked any cloud storage"));
            }

            ObjectId fileId               = ObjectId.GenerateNewId();
            var      serviceConfig        = user.CurrentBucket.BucketConfigData;
            var      googleBucketUploader = new RequestHandlerGoogleBucket(serviceConfig.ConfigData, serviceConfig.SelectedBucket);
            var      result               = googleBucketUploader.UploadFile(file, fileId.ToString(), encrypted, compressed);

            if (!result.Success)
            {
                return(BadRequest("Error while uploading your file!"));
            }

            var newFile = new DatabaseModule.Entities.File()
            {
                Id            = fileId,
                FileName      = file.FileName,
                FileType      = file.ContentType,
                FileSize      = file.Length,
                OwnerId       = Tools.AuthenticationManager.GetUserId(User),
                FolderId      = _folderId,
                Compressed    = compressed,
                Encrypted     = encrypted,
                EncryptionKey = result.EncryptionKey,
                IV            = result.IV,
                Favourite     = favourite,
                BucketId      = user.CurrentBucket.Id,
                AllowedUsers  = new List <ObjectId>()
            };
            await _filesRepository.InsertOneAsync(newFile);


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