Exemplo n.º 1
0
 public BlobFile2(FileEntity entity)
 {
     _username = entity.PartitionKey;
     _path = new FilePath(entity.Path);
     _entity = entity;
     _container = AzureServiceHelper.GetUserContainer(_username);
 }
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        //
        public void CreateFile(Stream stream)
        {
            ValidateFileNotExist();
            _entity = new FileEntity
            {
                PartitionKey = _username,
                RowKey = DateTime.UtcNow.Ticks.ToString("D") + _rnd.Next(1000, 9999).ToString("D"),
                Path = _path.Path(),
                Key = Guid.NewGuid().ToString("N"),
                Length = 0,
                Type = FileEntity.RegularFile,
                LastModified = DateTime.UtcNow,
                ContentType = MimeMapping.GetMimeMapping(_path.Name()),
                Parent = _path.BasePath()
            };

            GetBlob();
            _blob.UploadFromStream(stream);
            Properties();
            _entity.Length = _blob.Properties.Length;
            FileTable.Execute(TableOperation.Insert(_entity));
        }
Exemplo n.º 5
0
 public void CreateDirectory()
 {
     ValidateFileNotExist();
     _entity = new FileEntity
     {
         PartitionKey = _username,
         RowKey = DateTime.UtcNow.Ticks.ToString("D") + _rnd.Next(1000, 9999).ToString("D"),
         Path = _path.Path(),
         Key = Guid.NewGuid().ToString("N"),
         Length = 0,
         Type = FileEntity.Directory,
         LastModified = DateTime.UtcNow,
         ContentType = MimeMapping.GetMimeMapping(_path.Name()),
         Parent = _path.BasePath()
     };
     FileTable.Execute(TableOperation.Insert(_entity));
 }