Пример #1
0
        /// <summary>
        /// </summary>
        /// <param name="append"></param>
        /// <returns></returns>
        public Stream OpenWriteStream(bool append)
        {
            if (append)
            {
                throw new Exception("File Stream Append Not supported.");
            }

            using (var context = new OnlineFilesEntities())
            {
                File f = context.Files.FirstOrDefault(d => d.pk_FileId == ObjectGuid);
                return(f?.OpenWriteStream(PrincipleFactory.Instance.GetPrinciple(FromType.WebDav), ItemPath));
            }
        }
Пример #2
0
        /// <summary>
        /// Copies the fileId to this Folder.
        /// </summary>
        /// <param name="destination"></param>
        /// <param name="fileId"></param>
        /// <param name="destinationName"></param>
        /// <param name="user"></param>
        /// <param name="context"></param>
        /// <param name="dbcxtransaction"></param>
        public static void CopyFileHere(this Folder destination, Guid fileId, string destinationName, Principal user, OnlineFilesEntities context = null, DbContextTransaction dbcxtransaction = null)
        {
            bool createdContext     = false;
            bool createdTransaction = false;
            bool didRollback        = false;

            if (context == null)
            {
                createdContext = true;
                context        = new OnlineFilesEntities();
            }
            if (dbcxtransaction == null)
            {
                dbcxtransaction    = context.Database.BeginTransaction();
                createdTransaction = true;
            }
            try
            {
                var destSecurity = context.FolderSecurities.Where(d => d.fk_FolderId == destination.pk_FolderId);

                //Can the user create files at the destination location?
                if (destSecurity.Any(d => d.canCreateFiles && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId)))
                {
                    var file = context.Files
                               .Include(x => x.FileSecurities)
                               .Include(x => x.FileDatas)
                               .FirstOrDefault(d => d.pk_FileId == fileId);

                    //Can the user Read the file to be copied?
                    if (file.FileSecurities.Any(f => user.UserProfile.mySecurityGroups.Contains(f.SecurityObjectId) && f.canRead))
                    {
                        File newFile = File.Create(destination.pk_FolderId, file.Name, user.UserProfile, true);
                        context.SaveChanges();

                        //Copy the file data
                        using (Stream s = file.OpenReadStream(user))
                            using (Stream d = newFile.OpenWriteStream(user))
                                s.CopyTo(d);

                        //Copy the file Security
                        foreach (FileSecurity security in file.FileSecurities)
                        {
                            FileSecurity nsec = context.FileSecurities.FirstOrDefault(d => d.fk_FileId == newFile.pk_FileId && d.SecurityObjectId == security.SecurityObjectId);
                            if (nsec == null)
                            {
                                nsec = new FileSecurity()
                                {
                                    canRead          = security.canRead,
                                    canWrite         = security.canWrite,
                                    CanDelete        = security.CanDelete,
                                    fk_FileId        = newFile.pk_FileId,
                                    SecurityObjectId = security.SecurityObjectId
                                };
                                context.FileSecurities.Add(nsec);
                            }
                            else
                            {
                                nsec.canRead   = nsec.canRead || security.canRead;
                                nsec.CanDelete = nsec.CanDelete || security.CanDelete;
                                nsec.canWrite  = nsec.canWrite || security.canWrite;
                            }
                        }
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new SecurityException("Not Authorized.");
                    }
                }
                else
                {
                    throw new SecurityException("Not Authorized.");
                }
            }
            catch (Exception)
            {
                //If we got an error and we created the transaction, roll it back.
                if (createdTransaction)
                {
                    dbcxtransaction.Rollback();
                    didRollback = true;
                }
                throw;
            }

            finally
            {
                if (createdTransaction)
                {
                    //If we didn't roll back the transaction, commit it.
                    if (!didRollback)
                    {
                        dbcxtransaction.Commit();
                    }
                    dbcxtransaction.Dispose();
                }
                if (createdContext)
                {
                    context.Dispose();
                }
            }
        }