예제 #1
0
        public async Task <JsonResult> ViewAllFiles(CommonAddressModel model)
        {
            //Analyse app
            var app = await ApiService.ValidateAccessTokenAsync(model.AccessToken);

            var bucket = await _dbContext.Bucket.FindAsync(model.BucketId);

            //Security
            if (bucket.BelongingAppId != app.AppId)
            {
                return(this.Protocal(ErrorType.Unauthorized, "The bucket you tried to view is not that app's bucket."));
            }
            //Get all files.
            var allFiles = _dbContext.OSSFile.Include(t => t.BelongingBucket).Where(t => t.BucketId == bucket.BucketId).Take(200);

            foreach (var file in allFiles)
            {
                var path = _configuration["StoragePath"] + $@"{_}Storage{_}{file.BelongingBucket.BucketName}{_}{file.FileKey}.dat";
                file.JFileSize = new FileInfo(path).Length;
            }
            var viewModel = new ViewAllFilesViewModel
            {
                AllFiles = allFiles,
                BucketId = bucket.BucketId,
                message  = "Successfully get all your files of that bucket.",
                code     = ErrorType.Success
            };

            return(Json(viewModel));
        }
예제 #2
0
        public async Task <JsonResult> UploadFile(CommonAddressModel model)
        {
            var app = await APIService.ValidateAccessTokenAsync(model.AccessToken);

            //try find the target bucket
            var targetBucket = await _dbContext.Bucket.FindAsync(model.BucketId);

            if (targetBucket == null || targetBucket.BelongingAppId != app.AppId)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.Unauthorized,
                    message = "The bucket you try to upload is not your app's bucket!"
                }));
            }
            //try get the file from form
            var file = Request.Form.Files.First();

            if (file == null)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.InvalidInput,
                    message = "Please upload your file!"
                }));
            }
            //Test the extension
            bool validExtension = MIME.MIMETypesDictionary.ContainsKey(Path.GetExtension(file.FileName).Replace(".", ""));

            if (!validExtension)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.InvalidInput,
                    message = "The extension of your file is not supported!"
                }));
            }
            //Save to database
            var newFile = new OSSFile
            {
                RealFileName    = Path.GetFileName(file.FileName.Replace(" ", "")),
                FileExtension   = Path.GetExtension(file.FileName),
                BucketId        = targetBucket.BucketId,
                BelongingBucket = targetBucket,
            };

            _dbContext.OSSFile.Add(newFile);
            await _dbContext.SaveChangesAsync();

            //Try saving file.
            string DirectoryPath = GetCurrentDirectory() + $@"{_}Storage{_}{targetBucket.BucketName}{_}";

            if (Exists(DirectoryPath) == false)
            {
                CreateDirectory(DirectoryPath);
            }
            var fileStream = new FileStream(DirectoryPath + newFile.FileKey + ".dat", FileMode.Create);
            await file.CopyToAsync(fileStream);

            fileStream.Close();
            //Return json
            return(Json(new UploadFileViewModel
            {
                code = ErrorType.Success,
                FileKey = newFile.FileKey,
                message = "Successfully uploaded your file.",
                Path = newFile.GetInternetPath
            }));
        }