public WebResult AddSharing(int ownerId, int friendId, string folder)
        {
            WebResult result = new WebResult();

            var owner = (from u in db.Users where u.id == ownerId select u).SingleOrDefault();
            var friend = (from u in db.Users where u.id == friendId select u).SingleOrDefault();

            if (owner == null || friend == null)
                return new WebResult(WebResult.ErrorCodeList.USER_NOT_FOUND);
            string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), "upload_files", owner.username, folder);
            if (System.IO.Directory.Exists(path))
            {
                Sharing share = new Sharing()
                {
                    folder = folder,
                    idOwner = owner.id,
                    idRecevier = friendId,
                    user = owner.username
                };
                db.Sharings.InsertOnSubmit(share);
                db.SubmitChanges();
            }
            else
                return new WebResult(WebResult.ErrorCodeList.DIRECTORY_NOT_FOUND);
            return result;
        }
Exemplo n.º 2
0
        public WebResult Register(string username, string email, string password)
        {
            WebResult result = new WebResult();

            var users = from u in db.Users where u.username == username || u.email == email select u;
            if (username == "" || password == "" || email == "")
                result.ErrorCode = WebResult.ErrorCodeList.INFORMATION_REQUIRED;
            else if (users.Count() > 0)
                result.ErrorCode = WebResult.ErrorCodeList.USER_ALREADY_EXIST;
            if (result.ErrorCode != WebResult.ErrorCodeList.SUCCESS)
                return result;
            User user = new User()
            {
                username = username,
                email = email,
                password = password,
                superuser = false,
                root_path = username
            };
            string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), "upload_files", user.root_path);
            System.IO.Directory.CreateDirectory(path);
            db.Users.InsertOnSubmit(user);
            db.SubmitChanges();
            return result;
        }
        public WebResult<List<Sharing>> GetSharing(int userId)
        {
            WebResult<List<Sharing>> result = new WebResult<List<Sharing>>();
            var user = (from u in db.Users where u.id == userId select u).SingleOrDefault();

            if (user == default(User))
                return new WebResult<List<Sharing>>(WebResult.ErrorCodeList.USER_NOT_FOUND);

            var sharing = (from s in db.Sharings where s.idRecevier == userId select s).ToList();
            result.Value = sharing;
            return result;
        }
        public WebResult<List<SharingDefinition>> GetReceiver(int userId, string folder)
        {
            WebResult<List<SharingDefinition>> result = new WebResult<List<SharingDefinition>>();
            var user = (from u in db.Users where u.id == userId select u).SingleOrDefault();

            if (user == default(User))
                return new WebResult<List<SharingDefinition>>(WebResult.ErrorCodeList.USER_NOT_FOUND);
            string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), "upload_files", user.username, folder);
            if (System.IO.Directory.Exists(path) == false)
                return new WebResult<List<SharingDefinition>>(WebResult.ErrorCodeList.DIRECTORY_NOT_FOUND);
            var shares = from s in db.Sharings where s.idOwner == userId && s.folder == folder select s;
            result.Value = new List<SharingDefinition>();
            foreach (var share in shares)
            {
                try
                {
                    result.Value.Add(new SharingDefinition()
                    {
                        SharingId = share.id,
                        Username = (from u in db.Users where u.id == share.idRecevier select u.username).Single()
                    });
                }
                catch { }
            }
            return result;
        }
        public WebResult<FileDefinition> GetFileFrom(int userId, string folder, string file)
        {
            WebResult<FileDefinition> result = new WebResult<FileDefinition>();
            FileDefinition fd = new FileDefinition();
            result.Value = fd;
            var user = (from u in db.Users where u.id == userId select u).SingleOrDefault();

            if (user == default(User))
                return new WebResult<FileDefinition>(WebResult.ErrorCodeList.USER_NOT_FOUND);
            string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), "upload_files", user.username, folder, file);
            if (System.IO.File.Exists(path))
            {
                FileStream stream = new FileStream(path, FileMode.Open);
                fd.RawData = new byte[stream.Length];
                stream.Read(fd.RawData, 0, (int)stream.Length);
                fd.Filename = file;
                fd.MimeType = GetMimeType(fd.Filename);
                fd.FileUri = HttpContext.Current.Request.Url.ToString() + "/../../upload_files/" + user.username + "/" + folder + "/" + file;
            }
            else
                return new WebResult<FileDefinition>(WebResult.ErrorCodeList.FILE_NOT_FOUND);

            return result;
        }