예제 #1
0
        public static IActionResult Protocol(this ControllerBase controller, AiurProtocol model)
        {
            if (!controller.HttpContext.Response.HasStarted)
            {
                controller.HttpContext.Response.StatusCode = (int)model.ConvertToHttpStatusCode();
                return(new JsonResult(model));
            }

            return(new EmptyResult());
        }
예제 #2
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            if (context.HttpContext.WebSockets.IsWebSocketRequest)
            {
                return;
            }
            var arg = new AiurProtocol
            {
                Code    = ErrorType.InvalidInput,
                Message = "Wrong protocal!"
            };

            context.Result = new JsonResult(arg);
        }
예제 #3
0
 public AiurUnexpectedResponse(AiurProtocol response) : base(response.Message)
 {
     Response = response;
 }
예제 #4
0
 public UnexceptedResponse(AiurProtocol response) : base(response.Message)
 {
     Response = response;
 }
예제 #5
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."
            }));
        }