Exemplo n.º 1
0
        public async Task <CdnFileObject> UpdateObjectAsync(CdnFileObject fileObject)
        {
            using (var connection = GetConnection())
            {
                await connection.OpenAsync();

                var sqlText = @"UPDATE CDN_FileObject
                                SET Id=@Id, VersionId=@VersionId, ServerId=@ServerId, Size=@Size, 
                                    DateUploaded=@DateUploaded, LastAccess=@LastAccess, UploadId=@UploadId
                                WHERE Id=@Id AND VersionId=@VersionId AND ServerId=@ServerId";

                await connection.ExecuteAsync(sqlText,
                                              new
                {
                    fileObject.Id,
                    fileObject.VersionId,
                    fileObject.ServerId,
                    fileObject.Size,
                    fileObject.DateUploaded,
                    fileObject.LastAccess,
                    fileObject.UploadId
                }, commandType : CommandType.Text);

                return(fileObject);
            }
        }
        public async Task <CdnFileObject> UploadObjectAsync(string objectKey, Stream stream, int version)
        {
            var fileObject = await _fileObjectRepository.GetObjectByKeyAsync(objectKey, version);

            if (fileObject != null)
            {
                throw new ObjectKeyAlreadyExistsException($"Object with key '{objectKey}' and version '{version}' already exists.");
            }

            fileObject = new CdnFileObject
            {
                Id        = objectKey,
                VersionId = version,
                ServerId  = _cdnOptions.Server.Id
            };

            //For reserving object key
            await _fileObjectRepository.AddObjectAsync(fileObject);

            var outputObjectPath = $@"{BaseUploadFolder}\{objectKey}.{version}";

            using (var fileStream = File.OpenWrite(outputObjectPath))
            {
                await stream.CopyToAsync(fileStream);
            }

            fileObject.DateUploaded = DateTime.Now;
            fileObject.Size         = new FileInfo(outputObjectPath).Length;

            await _fileObjectRepository.UpdateObjectAsync(fileObject);

            return(fileObject);
        }
        /// <inheritdoc />
        public async Task <CdnFileObject> InitMultiPartUploadAsync(string objectKey, int version = 0)
        {
            var fileObject = await _fileObjectRepository.GetObjectByKeyAsync(objectKey, version);

            if (fileObject != null)
            {
                throw new ObjectKeyAlreadyExistsException($"Object with key '{objectKey}' and version '{version}' already exists.");
            }

            var uploadId = Guid.NewGuid();

            fileObject = new CdnFileObject
            {
                Id        = objectKey,
                ServerId  = _cdnOptions.Server.Id,
                UploadId  = uploadId,
                VersionId = version
            };

            await _fileObjectRepository.AddObjectAsync(fileObject);

            //Create directory for chunks
            Directory.CreateDirectory(GetChunkFolder(objectKey, uploadId.ToString()));

            return(fileObject);
        }
        public async Task <CachedObjectResult> GetObjectFromCache(CdnFileObject fileObject)
        {
            var fileName = $"{BaseCacheFolder}\\{fileObject.Id}.{fileObject.VersionId}";
            var result   = new CachedObjectResult();

            //Get fileObject in current server
            var currentServerfileObject = await _fileObjectRepository.GetObjectByKeyAsync(fileObject.Id, 0, _cdnOptions.Server.Id);

            if (currentServerfileObject != null)
            {
                currentServerfileObject.LastAccess = DateTime.Now;
                await _fileObjectRepository.UpdateObjectAsync(currentServerfileObject);

                result.IsSuccess  = true;
                result.FileStream = File.OpenRead(fileName);
                return(result);
            }

            //if object was not previously cached  and no space for downloading...
            if (Startup.CdnServer.FreeSpace < fileObject.Size)
            {
                result.IsSuccess = false;
                result.Exception = new NoFreeSpaceException();
                return(result);
            }

            //Get nearest origin server
            var server = await _cdnServerService.GetNearestServerAsync(_cdnOptions.Server.Longitude,
                                                                       _cdnOptions.Server.Latitude,
                                                                       CdnServerRole.OriginServer);

            var fileUrl = $@"{server.Host.Trim()}/api/download/{fileObject.Id}/{fileObject.VersionId}";

            //Download file object and save it locally...
            using (var client = new HttpClient())
            {
                //Hard code auth token...
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "49ec97b507cc21806861817f2f0564d4");
                using (var response = await client.GetAsync(fileUrl))
                    using (var fileStream = File.Create(fileName))
                    {
                        await response.Content.CopyToAsync(fileStream);
                    }
            }

            fileObject.ServerId   = _cdnOptions.Server.Id;
            fileObject.LastAccess = DateTime.Now;

            await _fileObjectRepository.AddObjectAsync(fileObject);

            result.IsSuccess  = true;
            result.FileStream = File.OpenRead(fileName);
            return(result);
        }
Exemplo n.º 5
0
        public async Task DeleteObjectAsync(CdnFileObject fileObject)
        {
            using (var connection = GetConnection())
            {
                await connection.OpenAsync();

                var sqlText = @"DELETE FROM CDN_FileObject
                                WHERE Id=@Id AND VersionId=@VersionId AND ServerId=@ServerId";

                await connection.ExecuteAsync(sqlText,
                                              new
                {
                    fileObject.Id,
                    fileObject.VersionId,
                    fileObject.ServerId
                }, commandType : CommandType.Text);
            }
        }
Exemplo n.º 6
0
        public async Task AddObjectAsync(CdnFileObject fileObject)
        {
            using (var connection = GetConnection())
            {
                await connection.OpenAsync();

                var sqlText = @"INSERT INTO CDN_FileObject(Id, VersionId, ServerId, Size, DateUploaded, LastAccess, UploadId)
                                VALUES (@Id, @VersionId, @ServerId, @Size, @DateUploaded, @LastAccess, @UploadId)";

                await connection.ExecuteAsync(sqlText,
                                              new
                {
                    fileObject.Id,
                    fileObject.VersionId,
                    fileObject.ServerId,
                    fileObject.Size,
                    fileObject.DateUploaded,
                    fileObject.LastAccess,
                    fileObject.UploadId
                }, commandType : CommandType.Text);
            }
        }