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

            if (folder == null)
            {
                return(this.Protocol(ErrorType.NotFound, "Locate folder failed!"));
            }
            return(Json(new AiurValue <Folder>(folder)
            {
                Code = ErrorType.Success,
                Message = "Successfully get your folder!"
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var site = await _dbContext
                       .Sites
                       .Include(t => t.Root)
                       .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName.ToLower());

            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 = _folderLocator.SplitToFolders(model.FolderNames);
            var folder  = await _folderLocator.LocateAsync(folders, site.Root, model.RecursiveCreate);

            if (folder == null)
            {
                return(this.Protocol(ErrorType.NotFound, $"Can't locate your folder!"));
            }
            try
            {
                var _ = HttpContext.Request.Form.Files.FirstOrDefault()?.ContentType;
            }
            catch (InvalidOperationException e)
            {
                return(this.Protocol(ErrorType.InvalidInput, e.Message));
            }
            // 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 File
            {
                FileName  = _folderLocator.GetValidFileName(folder.Files.Select(t => t.FileName), file.FileName), //file.FileName,
                ContextId = folder.Id,
                FileSize  = file.Length
            };
            // Save to disk
            await _storageProvider.Save(newFile.HardwareId, file);

            // Save to database
            _dbContext.Files.Add(newFile);
            await _dbContext.SaveChangesAsync();

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

            return(Json(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."
            }));
        }