/// <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> /// Open the File for Writing. /// </summary> /// <param name="length">New file length.</param> public void OpenWrite(long length) { this.length = length; try { // check access member = collection.GetMemberByID(accessID); // does the member exist? if (member == null) { throw new MemberDoesNotExistException(accessID); } // does the member have wright rights if ((member.Rights != Access.Rights.Admin) && (member.Rights != Access.Rights.ReadWrite)) { throw new AccessException(collection, member, Access.Rights.ReadWrite); } // backup file backupPath = String.Format("{0}.simias.temp", path); File.Copy(path, backupPath, true); long deltaSize = length - (new FileInfo(backupPath)).Length; // open file stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None); updating = true; // check file size policy FileSizeFilter fsFilter = FileSizeFilter.Get(collection); if (!fsFilter.Allowed(deltaSize)) { throw new FileSizeException(node.Name); } // check disk quota policy DiskSpaceQuota dsQuota = DiskSpaceQuota.Get(collection); if (!dsQuota.Allowed(deltaSize)) { throw new DiskQuotaException(node.Name); } // log log.LogAccess("OpenWrite", node.GetRelativePath(), node.ID, "Success"); } catch { // log log.LogAccess("OpenWrite", node.GetRelativePath(), node.ID, "Failed"); Close(true); throw; } }
/// <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; } } }