Exemplo n.º 1
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var folders = _folderLocator.SplitStrings(model.FolderNames);
            var folder  = await _folderLocator.LocateSiteAndFolder(model.AccessToken, model.SiteName, folders);

            var file    = Request.Form.Files.First();
            var newFile = new Pylon.Models.Probe.File
            {
                FileName  = Path.GetFileName(file.FileName).ToLower(),
                ContextId = folder.Id
            };

            //Ensure there not exists file with the same file name.
            lock (_obj)
            {
                var exists = folder.Files.Any(t => t.FileName == newFile.FileName);
                if (exists)
                {
                    return(this.Protocol(ErrorType.HasDoneAlready, "There already exists a file with that name."));
                }
                //Save to database
                _dbContext.Files.Add(newFile);
                _dbContext.SaveChanges();
            }
            //Try saving file.
            string directoryPath = _configuration["StoragePath"] + $"{_}Storage{_}";

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

                fileStream.Close();
            }
            return(this.Protocol(ErrorType.Success, "Successfully uploaded your file."));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var folders = _folderLocator.SplitStrings(model.FolderNames);
            var folder  = await _folderLocator.LocateSiteAndFolder(model.AccessToken, model.SiteName, folders, model.RecursiveCreate);

            var file = Request.Form.Files.First();

            if (!new ValidFolderName().IsValid(file.FileName))
            {
                var arg = new AiurProtocol()
                {
                    Code    = ErrorType.InvalidInput,
                    Message = $"Invalid file name: '{file.FileName}'!"
                };
            }
            var newFile = new Pylon.Models.Probe.File
            {
                FileName  = Path.GetFileName(file.FileName),
                ContextId = folder.Id
            };

            //Ensure there not exists file with the same file name.
            while (true)
            {
                var exists = folder.Files.Any(t => t.FileName == newFile.FileName);
                if (exists)
                {
                    newFile.FileName = "_" + newFile.FileName;
                }
                else
                {
                    //Save to database
                    _dbContext.Files.Add(newFile);
                    _dbContext.SaveChanges();
                    break;
                }
            }
            //Try saving file.
            var directoryPath = _configuration["StoragePath"] + $"{_}Storage{_}";

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

                fileStream.Close();
            }
            var filePath = $"{string.Join('/', model.FolderNames)}/{newFile.FileName}".TrimStart('/');
            var path     = $"{_serviceLocation.Probe}/Download/{nameof(DownloadController.Open)}/{model.SiteName.ToUrlEncoded()}/{filePath.EncodePath()}";

            return(Json(new UploadFileViewModel
            {
                InternetPath = path,
                SiteName = model.SiteName,
                FilePath = filePath,
                Code = ErrorType.Success,
                Message = "Successfully uploaded your file."
            }));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var site = await _dbContext
                       .Sites
                       .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName.ToLower());

            if (!site.OpenToUpload)
            {
                _tokenEnsurer.Ensure(model.PBToken, "Upload", model.SiteName, model.FolderNames);
            }
            var folders = _folderLocator.SplitStrings(model.FolderNames);
            var folder  = await _folderLocator.LocateSiteAndFolder(model.SiteName, folders, model.RecursiveCreate);

            // Executing here will let the browser upload the file.
            if (HttpContext.Request.Form.Files.Count < 1)
            {
                return(this.Protocol(ErrorType.InvalidInput, "Please provide a file!"));
            }
            var file = HttpContext.Request.Form.Files.First();

            if (!new ValidFolderName().IsValid(file.FileName))
            {
                return(this.Protocol(ErrorType.InvalidInput, $"Invalid file name: '{file.FileName}'!"));
            }
            var newFile = new Pylon.Models.Probe.File
            {
                FileName  = Path.GetFileName(file.FileName),
                ContextId = folder.Id
            };

            //Ensure there not exists file with the same file name.
            while (true)
            {
                var exists = folder.Files.Any(t => t.FileName == newFile.FileName);
                if (exists)
                {
                    newFile.FileName = "_" + newFile.FileName;
                }
                else
                {
                    //Save to database
                    _dbContext.Files.Add(newFile);
                    _dbContext.SaveChanges();
                    break;
                }
            }
            //Try saving file.
            var directoryPath = _configuration["StoragePath"] + $"{_}Storage{_}";

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

                fileStream.Close();
            }
            var filePath = StorageService.GetProbeFullPath(model.SiteName, string.Join('/', folders), newFile.FileName);
            var path     = StorageService.GetProbeDownloadAddress(_serviceLocation, filePath);

            return(Json(new UploadFileViewModel
            {
                InternetPath = path,
                SiteName = model.SiteName,
                FilePath = filePath,
                FileSize = file.Length,
                Code = ErrorType.Success,
                Message = "Successfully uploaded your file."
            }));
        }