Esempio n. 1
0
        public IHttpActionResult Create([FromBody] HttpPostedFileBase fileBase)
        {
            if (fileBase == null)
            {
                return(BadRequest("File null"));
            }

            if (fileBase.ContentLength <= 0)
            {
                return(BadRequest("File empty"));
            }

            var userId = Request.GetUserId();

            if (userId < 0)
            {
                return(Unauthorized());
            }

            var fileInfo  = new FileInfo(fileBase.FileName);
            var extension = fileInfo.Extension;

            var fileName = $"{Guid.NewGuid():N}.{extension}";

            var path = Path.Combine(FILE_LOCATION, PHOTOS_LOCATION, fileName);

            fileBase.SaveAs(path);

            string thumbnailPath = null;

            try
            {
                var img       = Image.FromFile(path);
                var thumbnail = (Image) new Bitmap(img, ThumbnailSize);

                thumbnailPath = Path.Combine(FILE_LOCATION, THUMBNAILS_LOCATION, fileName);
                thumbnail.Save(thumbnailPath);
            }
            catch
            {
                //todo log
            }

            var file = new File
            {
                RowState      = RowState.Created,
                Size          = fileBase.ContentLength * 1024,
                UploadedBy    = userId,
                UploadedAt    = DateTime.Now,
                Extension     = extension,
                FilePath      = path,
                ThumbnailPath = thumbnailPath,
                Name          = fileName
            };

            _fileManager.Add(file);

            return(Created($"/{file.Id}", Mapper.Map <FileViewModel>(file)));
        }
Esempio n. 2
0
        public IHttpActionResult CreateFromResult([FromBody] ResultFileUploadModel model)
        {
            var userId = Request.GetUserId();

            if (userId < 0)
            {
                return(Unauthorized());
            }

            if (model == null)
            {
                return(BadRequest("file is null"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var fileName = $"{Guid.NewGuid():N}.{model.Extension}";

            var directory          = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH);
            var directoryThumbnail = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH_THUMBNAIL);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            if (!Directory.Exists(directoryThumbnail))
            {
                Directory.CreateDirectory(directoryThumbnail);
            }


            var filePath = Path.Combine(directory, fileName);

            var filePathThumbnail = Path.Combine(directoryThumbnail, fileName);

            System.IO.File.WriteAllText(filePath, model.FileContent);

            //var image = Image.FromFile(filePath);

            //var thumbnail = (Image)new Bitmap(image, new Size(200, 200));

            //thumbnail.Save(filePathThumbnail);

            var urlPath          = GetUrlPath(FileManager.RELATIVE_PATH, fileName);
            var urlPathThumbnail = GetUrlPath(FileManager.RELATIVE_PATH_THUMBNAIL, fileName);

            var fileInfo = new FileInfo(filePath);

            var file = new File
            {
                Extension     = model.Extension,
                FilePath      = urlPath,
                ThumbnailPath = urlPathThumbnail,
                Size          = (int)(fileInfo.Length / 1024),
                RowState      = RowState.Created,
                UploadedAt    = DateTime.Now,
                UploadedBy    = userId
            };

            _fileManager.Add(file);

            return(Ok(Mapper.Map <FileViewModel>(file)));
        }
Esempio n. 3
0
        public IHttpActionResult CreateFromBase64([FromBody] ResultFileUploadModel model)
        {
            var userId = Request.GetUserId();

            if (userId < 0)
            {
                return(Unauthorized());
            }

            if (model == null)
            {
                return(BadRequest("file is null"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var strSplit = model.FileContent.Split(new[] { "base64," }, StringSplitOptions.None);

            if (strSplit.Length != 2)
            {
                return(BadRequest("invalid base64"));
            }

            var base64Str = strSplit[1];

            var fileName = $"{Guid.NewGuid():N}.{model.Extension}";

            var directory          = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH);
            var directoryThumbnail = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH_THUMBNAIL);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            if (!Directory.Exists(directoryThumbnail))
            {
                Directory.CreateDirectory(directoryThumbnail);
            }


            var filePath = Path.Combine(directory, fileName);

            var filePathThumbnail = Path.Combine(directoryThumbnail, fileName);

            var bytes = Convert.FromBase64String(base64Str);

            using (var imageFile = new FileStream(filePath, FileMode.Create))
            {
                imageFile.Write(bytes, 0, bytes.Length);
                imageFile.Flush();
            }

            var image = Image.FromFile(filePath);

            var thumbnail = (Image) new Bitmap(image, new Size(200, 200));

            thumbnail.Save(filePathThumbnail);

            var urlPath          = GetUrlPath(FileManager.RELATIVE_PATH, fileName);
            var urlPathThumbnail = GetUrlPath(FileManager.RELATIVE_PATH_THUMBNAIL, fileName);

            var fileInfo = new FileInfo(filePath);

            var file = new File
            {
                Extension     = model.Extension,
                FilePath      = urlPath,
                ThumbnailPath = urlPathThumbnail,
                Size          = (int)(fileInfo.Length / 1024),
                RowState      = RowState.Created,
                UploadedAt    = DateTime.Now,
                UploadedBy    = userId,
                Name          = fileName
            };

            _fileManager.Add(file);

            return(Ok(Mapper.Map <FileViewModel>(file)));
        }