Exemplo n.º 1
0
        public async Task<UploadResult> UploadFile(string fileName, string contentType, Stream stream)
        {
            var settings = _kernel.Get<ApplicationSettings>();

            var account = CloudStorageAccount.Parse(settings.AzureblobStorageConnectionString);
            var client = account.CreateCloudBlobClient();
            var container = client.GetContainerReference(JabbRUploadContainer);

            // Randomize the filename everytime so we don't overwrite files
            string randomFile = Path.GetFileNameWithoutExtension(fileName) +
                                "_" +
                                Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName);

            if (container.CreateIfNotExists())
            {
                // We need this to make files servable from blob storage
                container.SetPermissions(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomFile);
            blockBlob.Properties.ContentType = contentType;

            await Task.Factory.FromAsync((cb, state) => blockBlob.BeginUploadFromStream(stream, cb, state), ar => blockBlob.EndUploadFromStream(ar), null);

            var result = new UploadResult
            {
                Url = blockBlob.Uri.ToString(),
                Identifier = randomFile
            };

            return result;
        }
        public async Task<UploadResult> UploadFile(string fileName, string contentType, Stream stream)
        {
            ApplicationSettings settings = _settingsFunc();

            // Randomize the filename everytime so we don't overwrite files
            string randomFile = Path.GetFileNameWithoutExtension(fileName) +
                                "_" +
                                Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName);


            if (!Directory.Exists(settings.LocalFileSystemStoragePath))
            {
                Directory.CreateDirectory(settings.LocalFileSystemStoragePath);
            }

            // REVIEW: Do we need to validate the settings here out of paranoia?
            
            string targetFile = Path.GetFullPath(Path.Combine(settings.LocalFileSystemStoragePath, randomFile));

            using (FileStream destinationStream = File.Create(targetFile))
            {
                await stream.CopyToAsync(destinationStream);
            }


            var result = new UploadResult
            {
                Url = GetFullUrl(settings.LocalFileSystemStorageUriPrefix, randomFile),
                Identifier = randomFile
            };

            return result;
        }
Exemplo n.º 3
0
        public void AddAttachment(ChatMessage message, string fileName, string contentType, long size, UploadResult result)
        {
            var attachment = new Attachment
            {
                Id = result.Identifier,
                Url = result.Url,
                FileName = fileName,
                ContentType = contentType,
                Size = size,
                Room = message.Room,
                Owner = message.User,
                When = DateTimeOffset.UtcNow
            };

            _repository.Add(attachment);
        }