Ejemplo n.º 1
0
        public async Task <ActionResult <bool> > Delete()
        {
            try
            {
                var login    = Request.Headers["X-Login"];
                var password = Request.Headers["X-Token"];

                var auth = await _ethereumUserService.IsAuthenticatedAsyncCall(login, password);

                if (!auth)
                {
                    return(BadRequest(Errors.WRONG_CREDENTIALS));
                }

                return(Ok());
            }
            catch (Exception e)
            {
                return(HandleEthereumError(e.Message, e.StackTrace));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <FileDto> > Post([FromBody] FileDto request)
        {
            if (string.IsNullOrEmpty(request.Link))
            {
                return(BadRequest(Errors.REQUIRED_FIELDS_ARE_MISSING));
            }

            var login    = Request.Headers["X-Login"];
            var password = Request.Headers["X-Token"];

            var auth = await _ethereumUserService.IsAuthenticatedAsyncCall(login, password);

            if (!auth)
            {
                return(BadRequest(Errors.WRONG_CREDENTIALS));
            }

            var filePath = request.Link;

            if (!System.IO.File.Exists(filePath))
            {
                return(BadRequest(Errors.FILE_DOES_NOT_EXISTS));
            }

            var temp = new FileDto
            {
                Name        = Path.GetFileName(filePath),
                Size        = new FileInfo(filePath).Length,
                Description = request.Description ?? "",
                Created     = DateTime.Now,
                // Type = "image/jpeg", // set later
                // Link = "",           // set later
                // Modified,            // not needed
            };

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fileStream.CopyTo(ms);
                    var fileBytes = ms.ToArray();

                    temp.Type = MimeType.GetMimeType(fileBytes.Take(50).ToArray(), filePath);
                }

                var ipfsFile = _ipfsService.Add(temp.Name, fileStream);
                temp.Link = ipfsFile.Hash;
            }

            try
            {
                var etherFile = await _ethereumFileService.AddAsync(
                    login,
                    password,
                    temp.Type,
                    temp.Link,
                    temp.Size,
                    temp.Name,
                    temp.Description,
                    temp.Created);

                return(Ok(ConvertToDto(etherFile)));
            }
            catch (Exception e)
            {
                return(HandleEthereumError(e.Message));
            }
        }