Esempio n. 1
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var site = await _siteRepo.GetSiteByName(model.SiteName);

            if (site == null)
            {
                return(this.Protocol(ErrorType.NotFound, $"Can't find a site with name: '{model.SiteName}'!"));
            }
            if (!site.OpenToUpload)
            {
                _tokenEnsurer.Ensure(model.Token, "Upload", model.SiteName, model.FolderNames);
            }
            var folders    = _folderSplitter.SplitToFolders(model.FolderNames);
            var rootFolder = await _folderRepo.GetFolderFromId(site.RootFolderId);

            var folder = await _folderRepo.GetFolderFromPath(folders, rootFolder, model.RecursiveCreate);

            if (folder == null)
            {
                return(this.Protocol(ErrorType.NotFound, "Can't find your folder!"));
            }
            // Executing here will let the browser upload the file.
            try
            {
                var _ = HttpContext.Request.Form.Files.FirstOrDefault()?.ContentType;
            }
            catch (InvalidOperationException e)
            {
                return(this.Protocol(ErrorType.InvalidInput, e.Message));
            }
            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 fileName          = _folderSplitter.GetValidFileName(folder.Files.Select(t => t.FileName), file.FileName);
            var newFileHardwareId = await _fileRepo.SaveFileToDb(fileName, folder.Id, file.Length);

            await _storageProvider.Save(newFileHardwareId, file);

            var filePath = _probeLocator.GetProbeFullPath(model.SiteName, string.Join('/', folders), fileName);

            return(Ok(new UploadFileViewModel
            {
                InternetPath = _probeLocator.GetProbeOpenAddress(filePath),
                DownloadPath = _probeLocator.GetProbeDownloadAddress(filePath),
                SiteName = model.SiteName,
                FilePath = filePath,
                FileSize = file.Length,
                Code = ErrorType.Success,
                Message = "Successfully uploaded your file."
            }));
        }
Esempio n. 2
0
        public async Task <IActionResult> ViewContent(ViewContentAddressModel model)
        {
            var folders = _folderSplitter.SplitToFolders(model.FolderNames);
            var folder  = await _folderRepo.GetFolderAsOwner(model.AccessToken, model.SiteName, folders);

            if (folder == null)
            {
                return(this.Protocol(ErrorType.NotFound, "Locate folder failed!"));
            }
            return(this.Protocol(new AiurValue <Folder>(folder)
            {
                Code = ErrorType.Success,
                Message = "Successfully get your folder!"
            }));
        }