/// <summary> /// Create An iFolder Entry /// </summary> /// <param name="c"></param> /// <param name="parent"></param> /// <param name="type"></param> /// <param name="entryName"></param> /// <param name="path"></param> /// <returns></returns> internal static Node CreateEntry(Collection c, Node parent, iFolderEntryType type, string entryName, out string path, bool DontCheckPolicies) { Node result = null; // NOTE: a new entry off the iFolder is not allowed, it must be off the root directory node or lower if ((parent == null) || (c.ID.Equals(parent.ID))) { throw new EntryDoesNotExistException(parent.ID); } // NOTE: only directories can have children if (!parent.IsBaseType(NodeTypes.DirNodeType)) { throw new DirectoryEntryRequiredException(parent.ID); } // check the name CheckName(entryName); // create new path DirNode parentDirNode = (DirNode)parent; path = parentDirNode.GetFullPath(c); path = System.IO.Path.Combine(path, entryName); // check for existing entry (case insensitive test) if (SyncFile.DoesNodeExist(c, parentDirNode, entryName)) { throw new EntryAlreadyExistException(entryName); } // directory if (type == iFolderEntryType.Directory) { result = new DirNode(c, parentDirNode, entryName); } // file else { if (DontCheckPolicies == false) { // check file type policy FileTypeFilter filter = FileTypeFilter.Get(c); if (!filter.Allowed(entryName)) { throw new FileTypeException(entryName); } } result = new FileNode(c, parentDirNode, entryName); } return(result); }
/// <summary> /// Called to check if the file passes the policy. /// </summary> /// <param name="fNode">The node to check.</param> /// <returns>True if node passes policy.</returns> public bool Allowed(BaseFileNode fNode) { long fSize = fNode.Length; /* If the file is already present on the server, upload size would be * size of file on the server subtracted by size of file to upload */ Store stl = Store.GetStore(); Domain dom = stl.GetDomain(stl.DefaultDomain); Node n1 = dom.GetNodeByID(fNode.ID); if (n1 != null) { FileNode f1 = n1 as FileNode; if (f1.Length <= fSize) { fSize = fSize - f1.Length; } else { fSize = 0; } } if (!GroupDiskQuotaUploadAllowed(fSize)) { reason = PolicyType.Quota; return(false); } if (!dsQuota.Allowed(fSize)) { reason = PolicyType.Quota; return(false); } if (!fsFilter.Allowed(fSize)) { reason = PolicyType.Size; return(false); } if (!ftFilter.Allowed(fNode.GetFileName())) { reason = PolicyType.Type; return(false); } return(true); }
/// <summary> /// Create An iFolder Entry /// </summary> /// <param name="ifolderID">The iFolder ID</param> /// <param name="parentID">The Parent Entry ID</param> /// <param name="entryName">The New Entry Name</param> /// <param name="type">The iFolder Entry Type</param> /// <param name="accessID">The Access User ID</param> /// <returns>An iFolderEntry Object</returns> public static iFolderEntry CreateEntry(string ifolderID, string parentID, iFolderEntryType type, string entryName, string accessID, bool DontCheckPolicies) { Store store = Store.GetStore(); // collection Collection c = store.GetCollectionByID(ifolderID); if (c == null) { throw new iFolderDoesNotExistException(ifolderID); } // does member exist? Member member = c.GetMemberByID(accessID); if (member == null && Simias.Service.Manager.LdapServiceEnabled == true) { Domain domain = store.GetDomain(store.DefaultDomain); string[] IDs = domain.GetMemberFamilyList(accessID); foreach (string id in IDs) { member = c.GetMemberByID(id); if (member != null) { break; } } } if (member == null) { throw new MemberDoesNotExistException(accessID); } // impersonate iFolder.Impersonate(c, accessID); Node parent = c.GetNodeByID(parentID); string path; Node entry = CreateEntry(c, parent, type, entryName, out path, DontCheckPolicies); // directory if (type == iFolderEntryType.Directory) { try { // create directory and node DirectoryInfo info = Directory.CreateDirectory(path); // update (entry as DirNode).CreationTime = info.CreationTime; c.Commit(entry); } catch { if (Directory.Exists(path)) { Directory.Delete(path); } throw; } } // file else { if (DontCheckPolicies == false) { // check file type policy FileTypeFilter filter = FileTypeFilter.Get(c); if (!filter.Allowed(entryName)) { throw new FileTypeException(entryName); } } try { // create the file and node File.Create(path).Close(); // update (entry as FileNode).UpdateFileInfo(c); c.Commit(entry); } catch { if (File.Exists(path)) { File.Delete(path); } throw; } } // open the new node Node n = c.GetNodeByID(entry.ID); return(iFolderEntry.GetEntry(c, n)); }
/// <summary> /// Process the Request /// </summary> /// <param name="context">The HttpContext object.</param> public override void ProcessRequest(HttpContext context) { const int BUFFERSIZE = (16 * 1024); string backupPath = null; try { bool DontCheckPolicies = false; // initialize Initialize(context); Length = int.Parse(context.Request.QueryString["Length"]); string dontCheckPolicies = null; long NodeLength = Length; try { dontCheckPolicies = context.Request.QueryString["DontCheckPolicies"]; if (dontCheckPolicies != null && dontCheckPolicies == "true") { DontCheckPolicies = true; } } catch { } try { string nodelength = context.Request.QueryString["NodeLength"]; if (nodelength != null && nodelength != string.Empty) { Length = int.Parse(nodelength); } } catch { } // does member have write rights if ((member.Rights != Access.Rights.Admin) && (member.Rights != Access.Rights.ReadWrite)) { throw new AccessException(collection, member, Access.Rights.ReadWrite); } long backupLength = 0; // new file? if (node == null) { filename = System.IO.Path.GetFileName(entryPath); Node parent = iFolderEntry.GetEntryByPath(collection, System.IO.Path.GetDirectoryName(entryPath).Replace('\\', '/')); node = (FileNode)iFolderEntry.CreateEntry(collection, parent, iFolderEntryType.File, filename, out filePath, DontCheckPolicies); } else { // check file type policy FileTypeFilter fsFilter = FileTypeFilter.Get(collection); if (!fsFilter.Allowed(filename)) { throw new FileTypeException(filename); } // backup file backupPath = String.Format("{0}.simias.temp", filePath); File.Copy(filePath, backupPath, true); backupLength = (new FileInfo(backupPath)).Length; } long deltaSize = context.Request.ContentLength - backupLength; if (DontCheckPolicies == false) { if (collection.Disabled) { throw new LockException(); } // Check first, if this file is violating aggregate disk quota limit set to his group if (!iFolderUser.GroupQuotaUploadAllowed(collection.Owner.UserID, deltaSize)) { throw new DiskQuotaException(filename); } // check file size policy FileSizeFilter fsFilter = FileSizeFilter.Get(collection); if (!fsFilter.Allowed(deltaSize)) { throw new FileSizeException(filename); } // check disk quota policy DiskSpaceQuota dsQuota = DiskSpaceQuota.Get(collection); if (!dsQuota.Allowed(deltaSize)) { throw new DiskQuotaException(filename); } } try{ // lock the file FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None); // reader Stream reader = context.Request.InputStream; try { byte[] buffer = new byte[BUFFERSIZE]; int count = 0; // download file while ((count = reader.Read(buffer, 0, BUFFERSIZE)) > 0) { stream.Write(buffer, 0, count); stream.Flush(); } } finally { // release the file stream.Close(); // release the reader reader.Close(); } // update node node.UpdateWebFileInfo(collection, Length); //node.UpdateFileInfo(collection); collection.Commit(node); /* * As of now the hash map from web access is being uploaded only for unencrypted files. So for encrypted files, if we are doing delta sync, then the existing hashmap should be removed immediately after the file is uploaded ( because we are not uploading the new hashmap for the file.) */ /* Uploading Hash map for unencrypted files */ if (collection.EncryptionAlgorithm == null || collection.EncryptionAlgorithm == "") { // Upload hashmap for unencrypted iFolders.. log.LogAccess("Upload hash", node.GetRelativePath(), node.ID, "Success"); HashMap map = new HashMap(collection, node); map.CreateHashMapFile(); } // log log.LogAccess("Upload", node.GetRelativePath(), node.ID, "Success"); } catch { // restore backup if (backupPath != null) { File.Copy(backupPath, filePath, true); } // log log.LogAccess("Upload", node.GetRelativePath(), node.ID, "Failed"); throw; } finally { // delete backup file if ((backupPath != null) && File.Exists(backupPath)) { File.Delete(backupPath); backupPath = null; } } } catch (Exception e) { // consume the file for better error reporting Stream reader = null; try { // reader reader = context.Request.InputStream; byte[] buffer = new byte[BUFFERSIZE]; // download file while (reader.Read(buffer, 0, BUFFERSIZE) > 0) { ; } } catch { // ignore } finally { // release the reader if (reader != null) { reader.Close(); } } // create an HTTP error context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.StatusDescription = e.GetType().Name; } finally { // delete backup file if ((backupPath != null) && File.Exists(backupPath)) { File.Delete(backupPath); backupPath = null; } } }