public async Task <ActionResult <BlobEntity> > DownloadFile(string id)
        {
            BlobEntity blobItem = FilesStorage.Find(item =>
                                                    item.fileId.Equals(id, StringComparison.InvariantCultureIgnoreCase));

            if (blobItem == null)
            {
                return(NotFound());
            }

            //download the file to the local server
            await DownloadBlobFileToServer(blobItem);

            var net = new System.Net.WebClient();

            try
            {
                var data    = net.DownloadData(blobItem.pathFile);
                var content = new System.IO.MemoryStream(data);

                if (System.IO.File.Exists(blobItem.pathFile))
                {
                    System.IO.File.Delete(blobItem.pathFile);
                }

                return(File(data, "application/octet-stream", blobItem.fileName));
            }
            catch
            {
                return(Conflict("The wanted file couldn't be found or accessed!"));
            }
        }
        public BlobEntity Create(BlobEntity blobEntity, int userId)
        {
            using (var connection = _dbConnectionFactory.CreateConnection())
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    var parameters = new DynamicParameters();

                    parameters.Add("@Id", blobEntity.Id, DbType.Guid);
                    parameters.Add("@Folder", blobEntity.Folder, DbType.String);
                    parameters.Add("@Extension", blobEntity.Extension, DbType.String);
                    parameters.Add("@ContentType", blobEntity.ContentType, DbType.String);
                    parameters.Add("@ContentLength", blobEntity.ContentLength, DbType.Int64);
                    parameters.Add("@SourceFileName", blobEntity.SourceFileName, DbType.String);
                    parameters.Add("@UTCCreated", blobEntity.UTCCreated, DbType.DateTime2);

                    parameters.Add("@UserId", userId, DbType.Int32);

                    int result = connection.Execute(
                        sql: "BlobsInsert",
                        commandType: CommandType.StoredProcedure,
                        transaction: transaction,
                        param: parameters
                        );

                    transaction.Commit();

                    return(blobEntity);
                }
            }
        }
Exemple #3
0
        public BlobPage()
        {
            InitializeComponent();

            _newBlob = new BlobEntity();

            InitializeDataBinding();
        }
        public ElastoBlob(BlobEntity blob) : base(blob.Blob, blob.Contour)
        {
            int leftContoursTopIndex;
            int rightContoursTopIndex;
            int leftContoursBottomIndex  = LeftContoursBottomIndexes(blob.Contour, out leftContoursTopIndex);
            int rightContoursBottomIndex = RightContoursBottomIndexes(blob.Contour, out rightContoursTopIndex);

            LeftContour  = new Contour(blob.Contour.Points.GetRange(leftContoursBottomIndex, leftContoursTopIndex - leftContoursBottomIndex));
            RightContour = new Contour(blob.Contour.Points.GetRange(rightContoursTopIndex, rightContoursBottomIndex - rightContoursTopIndex));
        }
        /// <summary>
        /// Add a new blob entity.
        /// </summary>
        /// <param name="blob">Blob to add.</param>
        /// <returns></returns>
        public async Task <bool> AddBlobEntity(BlobEntity blob)
        {
            await CreateIfNotExists();

            CloudBlockBlob blockBlob = _blobContainer.GetBlockBlobReference(blob.BlobName);

            await blockBlob.UploadTextAsync(blob.Text);

            return(true);
        }
        public ActionResult Update(BlobEntity newFile)
        {
            var oldFile = FilesStorage.Find(
                item => item.fileId.Equals
                    (newFile.fileId, StringComparison.InvariantCultureIgnoreCase));

            if (oldFile == null)
            {
                return(BadRequest("Cannot update as blob doesn't exist!"));
            }

            oldFile.fileName    = newFile.fileName;
            oldFile.description = newFile.description;
            return(Ok());
        }
        private async Task DownloadBlobFileToServer(BlobEntity blobItem)
        {
            try{
                string         fileName = Path.GetFileName(blobItem.pathFile);
                CloudBlockBlob blockBob = AzureConnection.Container.GetBlockBlobReference(fileName);
                var            rootDir  = new FileInfo(blobItem.pathFile).Directory;
                if (!rootDir.Exists) //make sure the parent directory exists
                {
                    rootDir.Create();
                }

                await blockBob.DownloadToFileAsync(blobItem.pathFile, FileMode.Create);
            }
            catch {
                System.Diagnostics.Debug.WriteLine("File couldn't be downloaded from the Azure!");
            }
        }
        public BlobEntity Upload(string sourceFileName, Stream sourceFileStream, int userId)
        {
            var blobInfo      = new BlobInfo(sourceFileName);
            var contentLength = sourceFileStream.Length;

            var blobEntity = new BlobEntity()
            {
                Id             = blobInfo.Id,
                Folder         = blobInfo.Folder,
                Extension      = blobInfo.Extension,
                ContentType    = blobInfo.ContentType,
                ContentLength  = contentLength,
                SourceFileName = sourceFileName,
                UTCCreated     = DateTime.UtcNow
            };

            _storageService.WriteStream(blobInfo.Name, sourceFileStream);

            blobEntity = _blobsRepository.Create(blobEntity, userId);

            return(blobEntity);
        }
        private void GetSevenZipData(string filename, DumpEntity dump)
        {
            var readerOptions = new ReaderOptions
            {
                LookForHeader = true
            };

            var archive = SevenZipArchive.Open(filename, readerOptions);

            foreach (SevenZipArchiveEntry entry in archive.Entries)
            {
                var blob = new BlobEntity
                {
                    Name = entry.Key,
                    Size = (ulong)entry.Size
                };

                blob.Hashes.Add("crc32", entry.Crc.ToString("x8"));

                dump.Blobs.Add(blob);
            }
        }
        private BlobEntity ReadBlob(XmlNode node)
        {
            var result = new BlobEntity();

            if (node.Attributes != null)
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    switch (attribute.Name)
                    {
                    case "name":
                        result.Name = attribute.Value;
                        break;

                    case "size":
                        result.Size = ulong.Parse(attribute.Value);
                        break;

                    case "crc":
                        result.Hashes.Add("crc32", attribute.Value);
                        break;

                    case "md5":
                        result.Hashes.Add("md5", attribute.Value);
                        break;

                    case "sha1":
                        result.Hashes.Add("sha1", attribute.Value);
                        break;

                    default:
                        throw new NotSupportedException($"Rom: attribute {attribute.Name} not supported.");
                    }
                }
            }

            return(result);
        }
Exemple #11
0
        public void MainTest()
        {
            var blobData = new byte[] { 0, 1 };

            using (Domain.OpenSession()) {
                using (var transactionScope = Session.Current.OpenTransaction()) {
                    var blob = new BlobEntity();

                    blob.BlobData = blobData;

                    transactionScope.Complete();
                }
            }

            using (Domain.OpenSession()) {
                using (var transactionScope = Session.Current.OpenTransaction()) {
                    var blob = Query.All <BlobEntity>().Single();
                    Assert.AreEqual(blobData.Length, blob.BlobData.Length);
                    Assert.AreEqual(blobData, blob.BlobData);
                    transactionScope.Complete();
                }
            }
        }
        public async Task <IActionResult> CreateBlobItem([FromForm] CreateBlob postData)
        {
            string     id       = this.GenerateId().ToString();
            BlobEntity blobItem = BlobItemFactory.Create(postData, id, blobItemsPath);

            if (blobItem == null)
            {
                return(Conflict("Error! Maybe you did not gave us all the needed info?"));
            }

            string fileName = Path.GetFileName(blobItem.pathFile);

            CloudBlockBlob blockBob = AzureConnection.Container.GetBlockBlobReference(fileName);

            using (Stream fileStream = postData.file.OpenReadStream())
            {
                await blockBob.UploadFromStreamAsync(fileStream);

                FilesStorage.Add(blobItem); //set file in the "db"
            }

            return(Created("", blobItem));
        }
        public IEnumerable <BlobEntity> SplitChunks(
            Guid blobId, byte[] originalBytes, string hash, string remark, long timeStamp)
        {
            // 压缩分块之前的内容
            var bodyBytes      = ArchiveUtils.CompressToGZip(originalBytes);
            var chunkIndex     = 0;
            var bodyBytesStart = 0;

            // 对内容进行分块
            do
            {
                var entityBodySize = Math.Min(bodyBytes.Length - bodyBytesStart, BlobEntity.BlobChunkSize);
                var entity         = new BlobEntity();
                entity.Id         = PrimaryKeyUtils.Generate <Guid>();
                entity.BlobId     = blobId;
                entity.ChunkIndex = chunkIndex++;
                entity.Remark     = remark;
                if (bodyBytesStart == 0 && entityBodySize == bodyBytes.Length)
                {
                    // 不需要分块
                    entity.Body = bodyBytes;
                }
                else
                {
                    entity.Body = new byte[entityBodySize];
                    Array.Copy(bodyBytes, bodyBytesStart, entity.Body, 0, entityBodySize);
                }
                bodyBytesStart  += entityBodySize;
                entity.TimeStamp = timeStamp == 0 ?
                                   DateTime.UtcNow :
                                   Mapper.Map <long, DateTime>(timeStamp);
                entity.BodyHash   = hash;
                entity.CreateTime = DateTime.UtcNow;
                yield return(entity);
            } while (bodyBytesStart < bodyBytes.Length);
        }
 public BlobUploadResult(BlobEntity blobEntity, BlobInfo blobInfo)
 {
     Entity = blobEntity;
     Info   = blobInfo;
 }