Exemplo n.º 1
0
        public HttpResponseMessage Get(String itemId)
        {
            var    user = _userSvc.CurrentUser();
            Attach file = _attach.GetById(Guid.Parse(itemId));

            if (file != null && file.UserId != user.Id)
            {
                throw new Exception();
            }

            var key  = HttpContext.Current.Session["FILEKEY"].ToString();
            var data = _crypt.DecryptBytes(file.Data, key);


            var result = new HttpResponseMessage
            {
                Content = new ByteArrayContent(data)
            };

            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
            {
                FileName = file.Name
            };
            var mime = MimeTypes.GetMimeType(file.Name);

            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mime);
            return(result);
        }
Exemplo n.º 2
0
        public Guid CreateNew(LoadFileModel model)
        {
            if (!model.FileName.ToLowerInvariant().EndsWith(".xml"))
            {
                model.FileName += ".xml";
            }

            var user = _userSvc.CurrentUser();

            Repos.File file          = _files.GetByIdUserAndLabel(user.Id, model.FileName);
            var        encriptionKey = model.FileKey;

            var crys  = new CryptoService();
            var empty = crys.Empty;
            var root  = crys.Initialize(empty);

            var strData   = crys.Save();
            var encrypted = Encoding.UTF8.GetBytes(_crypt.Encrypt(strData, encriptionKey));

            if (file == null)
            {
                file = new Repos.File
                {
                    Id      = Guid.NewGuid(),
                    Label   = model.FileLabel,
                    Name    = model.FileName,
                    UserId  = user.Id,
                    Content = encrypted
                };
                _files.Add(file);
            }
            else
            {
                file.Content = encrypted;
                _files.Update(file);
            }
            _attach.DeleteFile(file.Id);
            foreach (var singleFile in root.Attachments)
            {
                //_attach.DeleteFile(Guid.Parse(singleFile.Id));
                var toAdd = new Attach
                {
                    Id     = Guid.Parse(singleFile.Id),
                    UserId = user.Id,
                    FileId = file.Id,
                    Name   = file.Name,
                    Data   = singleFile.Data
                };
                _attach.Add(toAdd);
            }
            return(file.Id);
        }
Exemplo n.º 3
0
        public IEnumerable <User> Get()
        {
            User current = _login.CurrentUser();

            if (!current.IsAdmin)
            {
                var res = _users.GetById(current.Id);
                res.Password = null;
                yield return(res);
            }
            else
            {
                foreach (var user in _users.GetAll())
                {
                    user.Password = null;
                    yield return(user);
                }
            }
        }
Exemplo n.º 4
0
        private void Save()
        {
            var user      = _userSvc.CurrentUser();
            var file      = _files.GetByIdUserAndLabel(user.Id, HttpContext.Current.Session["FILE"].ToString());
            var data      = HttpContext.Current.Session["DATA"] as CryptoService;
            var dataSaved = data.Save();
            var cipher    = new StringCipher();
            var encrypted = cipher.Encrypt(dataSaved,
                                           HttpContext.Current.Session["FILEKEY"].ToString());

            file.Content = Encoding.UTF8.GetBytes(encrypted);
            _files.Update(file);
        }