public BlobFile2(string username, string path) { _username = username; _path = new FilePath(path); var query = new TableQuery<FileEntity>().Where( TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, _username), TableOperators.And, TableQuery.GenerateFilterCondition("Path", QueryComparisons.Equal, _path.Path()))); try { _entity = FileTable.ExecuteQuery(query).FirstOrDefault(); } catch (Exception ex) { _entity = null; } if (_entity == null && _path.IsRoot()) CreateDirectory(); _container = AzureServiceHelper.GetUserContainer(username); }
public int Copy(string p) { if (_path.Contains(p)) throw new Exception("You can't copy a directory into itself"); var target = new BlobFile2(_username, p); if (target.Exists()) { if (target.IsDirectory()) { p += "/" + Path().Name(); } else { target.ValidateFileNotExist(); } } new BlobFile2(_username, p).ValidateFileNotExist(); var operations = new TableBatchOperation(); var path = new FilePath(p); if (IsDirectory()) { foreach (FileEntity i in FileTable.ExecuteQuery(PrefixQuery())) { var n = new FileEntity() { ContentType = i.ContentType, Key = Guid.NewGuid().ToString("N"), LastModified = DateTime.UtcNow, Length = i.Length, Path = i.Path.Replace(_path.Path(), path.Path()), PartitionKey = i.PartitionKey, Parent = i.Parent.Replace(_path.Path(), path.Path()), RowKey = DateTime.UtcNow.Ticks.ToString("D") + _rnd.Next(1000, 9999).ToString("D"), Type = i.Type }; operations.Add(TableOperation.Insert(n)); if (i.Type != FileEntity.Directory) _container.GetBlockBlobReference(n.Key) .StartCopyFromBlob(_container.GetBlockBlobReference(i.Key)); } } var m = new FileEntity() { ContentType = _entity.ContentType, Key = Guid.NewGuid().ToString("D"), LastModified = DateTime.UtcNow, Length = _entity.Length, Path = path.Path(), PartitionKey = _entity.PartitionKey, Parent = path.BasePath(), RowKey = DateTime.UtcNow.Ticks.ToString("N") + _rnd.Next(1000, 9999).ToString("D"), Type = _entity.Type }; if (!IsDirectory()) { _container.GetBlockBlobReference(m.Key).StartCopyFromBlob(GetBlob()); } operations.Add(TableOperation.Insert(m)); FileTable.ExecuteBatch(operations); return operations.Count; }
public int Move(string p) { if (_path.Contains(p)) throw new Exception("You can't move a directory into itself"); var target = new BlobFile2(_username, p); if (target.Exists()) { if (target.IsDirectory()) { p += "/" + Path().Name(); } else { target.ValidateFileNotExist(); } } new BlobFile2(_username, p).ValidateFileNotExist(); var operations = new TableBatchOperation(); var path = new FilePath(p); if (IsDirectory()) { foreach (FileEntity i in FileTable.ExecuteQuery(PrefixQuery())) { i.Path = i.Path.Replace(_path.Path(), path.Path()); i.Parent = i.Parent.Replace(_path.Path(), path.Path()); operations.Add(TableOperation.Replace(i)); } } _entity.Path = path.Path(); _entity.Parent = path.BasePath(); operations.Add(TableOperation.Replace(_entity)); this._path = path; FileTable.ExecuteBatch(operations); return operations.Count; }