protected override async Task CopyFromSource(ILogger log, CancellationToken token) { if (await _blob.ExistsAsync()) { log.LogInformation($"GET {_blob.Uri.AbsoluteUri}"); if (File.Exists(LocalCacheFile.FullName)) { LocalCacheFile.Delete(); } using (var cache = File.OpenWrite(LocalCacheFile.FullName)) { await _blob.DownloadToStreamAsync(cache); } // If the blob is compressed it needs to be decompressed locally before it can be used if (_blob.Properties.ContentEncoding?.Equals("gzip", StringComparison.OrdinalIgnoreCase) == true) { log.LogInformation($"Decompressing {_blob.Uri.AbsoluteUri}"); var gzipFile = LocalCacheFile.FullName + ".gz"; File.Move(LocalCacheFile.FullName, gzipFile); using (Stream destination = File.Create(LocalCacheFile.FullName)) using (Stream source = File.OpenRead(gzipFile)) using (Stream zipStream = new GZipStream(source, CompressionMode.Decompress)) { zipStream.CopyTo(destination); } } } }
/// <summary> /// Copy the file to the destination path. /// </summary> /// <returns>True if the file was successfully copied.</returns> public async Task <bool> CopyTo(string path, bool overwrite, ILogger log, CancellationToken token) { var pathInfo = new FileInfo(path); if (!overwrite && pathInfo.Exists) { return(false); } // Download the file if needed. await EnsureFile(log, token); // Check if the local copy exists if (File.Exists(LocalCacheFile.FullName)) { // Create the parent dir pathInfo.Directory.Create(); // Copy the file LocalCacheFile.CopyTo(pathInfo.FullName, overwrite); return(true); } return(false); }
protected override async Task CopyToSource(ILogger log, CancellationToken token) { var absoluteUri = UriUtility.GetPath(RootPath, key); if (!File.Exists(LocalCacheFile.FullName)) { if (await FileExistsAsync(client, bucketName, key, token).ConfigureAwait(false)) { log.LogVerbose($"Removing {absoluteUri}"); await RemoveFileAsync(client, bucketName, key, token).ConfigureAwait(false); } else { log.LogVerbose($"Skipping {absoluteUri}"); } return; } log.LogVerbose($"Pushing {absoluteUri}"); using (var cache = LocalCacheFile.OpenRead()) { Stream writeStream = cache; string contentType = null, contentEncoding = null; if (key.EndsWith(".nupkg", StringComparison.Ordinal)) { contentType = "application/zip"; } else if (key.EndsWith(".xml", StringComparison.Ordinal) || key.EndsWith(".nuspec", StringComparison.Ordinal)) { contentType = "application/xml"; } else if (key.EndsWith(".json", StringComparison.Ordinal) || await JsonUtility.IsJsonAsync(LocalCacheFile.FullName)) { contentType = "application/json"; contentEncoding = "gzip"; // Compress content before uploading log.LogVerbose($"Compressing {absoluteUri}"); writeStream = await JsonUtility.GZipAndMinifyAsync(cache); } else if (key.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || key.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase)) { contentType = "application/octet-stream"; } else { log.LogWarning($"Unknown file type: {absoluteUri}"); } await UploadFileAsync(client, bucketName, key, contentType, contentEncoding, writeStream, token) .ConfigureAwait(false); writeStream.Dispose(); } }
protected override async Task CopyToSource(ILogger log, CancellationToken token) { if (File.Exists(LocalCacheFile.FullName)) { log.LogInformation($"Pushing {_blob.Uri.AbsoluteUri}"); using (var cache = LocalCacheFile.OpenRead()) { Stream writeStream = cache; if (_blob.Uri.AbsoluteUri.EndsWith(".nupkg", StringComparison.Ordinal)) { _blob.Properties.ContentType = "application/zip"; } else if (_blob.Uri.AbsoluteUri.EndsWith(".xml", StringComparison.Ordinal) || _blob.Uri.AbsoluteUri.EndsWith(".nuspec", StringComparison.Ordinal)) { _blob.Properties.ContentType = "application/xml"; } else if (_blob.Uri.AbsoluteUri.EndsWith(".json", StringComparison.Ordinal) || JsonUtility.IsJson(LocalCacheFile.FullName)) { _blob.Properties.ContentType = "application/json"; _blob.Properties.ContentEncoding = "gzip"; // Compress content before uploading log.LogInformation($"Compressing {_blob.Uri.AbsoluteUri}"); writeStream = GZipAndMinify(cache); } else { log.LogWarning($"Unknown file type: {_blob.Uri.AbsoluteUri}"); } await _blob.UploadFromStreamAsync(writeStream); writeStream.Dispose(); } _blob.Properties.CacheControl = "no-store"; // TODO: re-enable this once it works again. _blob.Properties.ContentMD5 = null; await _blob.SetPropertiesAsync(); } else if (await _blob.ExistsAsync()) { log.LogInformation($"Removing {_blob.Uri.AbsoluteUri}"); await _blob.DeleteAsync(); } else { log.LogInformation($"Skipping {_blob.Uri.AbsoluteUri}"); } }
public async Task <Stream> GetStream(ILogger log, CancellationToken token) { await EnsureFile(log, token); if (LocalCacheFile.Exists) { return(LocalCacheFile.OpenRead()); } else { return(null); } }
public Task Write(JObject json, ILogger log, CancellationToken token) { _downloaded = true; _hasChanges = true; if (File.Exists(LocalCacheFile.FullName)) { LocalCacheFile.Delete(); } JsonUtility.SaveJson(LocalCacheFile, json); return(Task.FromResult(true)); }
// Code that runs on entering the state. public override void OnEnter() { LocalCacheFile sendfile = new LocalCacheFile() { path = HttpUrl.Value, isURLSign = isURLSign.Value, sign = Sign.Value, hasPrefix = hasPrefix.Value, isKOD = false, }; MessageDispatcher.AddListener(VrDispMessageType.GetLocalCacheFile.ToString(), GetCacheFile); MessageDispatcher.SendMessage(this, VrDispMessageType.SendCacheFile.ToString(), sendfile, 0.01f); }
void GetCacheFile(IMessage msg) { LocalCacheFile sendfile = msg.Data as LocalCacheFile; LocalPath.Value = sendfile.path; LocalUrl.Value = "File://" + LocalPath; GetedSign.Value = sendfile.sign; if (sendfile.path != "") { Fsm.Event(GetLocalPath); } else { Fsm.Event(GetLocalPathFaild); } }
protected override Task CopyToSource(ILogger log, CancellationToken token) { if (File.Exists(LocalCacheFile.FullName)) { log.LogInformation($"Pushing {_sourceFile.FullName}"); _sourceFile.Directory.Create(); var tmp = _sourceFile.FullName + ".tmp"; if (File.Exists(tmp)) { // Clean up tmp file File.Delete(tmp); } LocalCacheFile.CopyTo(tmp); if (File.Exists(_sourceFile.FullName)) { // Clean up old file File.Delete(_sourceFile.FullName); } File.Move(tmp, _sourceFile.FullName); } else if (File.Exists(_sourceFile.FullName)) { log.LogInformation($"Removing {_sourceFile.FullName}"); _sourceFile.Delete(); if (!Directory.EnumerateFiles(_sourceFile.DirectoryName).Any() && !Directory.EnumerateDirectories(_sourceFile.DirectoryName).Any()) { // Remove the parent directory if it is now empty log.LogInformation($"Removing {_sourceFile.DirectoryName}"); _sourceFile.Directory.Delete(); } } else { log.LogInformation($"Skipping {_sourceFile.FullName}"); } return(Task.FromResult(true)); }
void GetCacheFile(IMessage msg) { LocalCacheFile sendfile = msg.Data as LocalCacheFile; LocalPath.Value = sendfile.path; LocalUrl.Value = "File://" + LocalPath; GetedSign.Value = sendfile.sign; if (sendfile.sign == Sign.Value) { if (sendfile.path != "") { Fsm.Event(GetLocalPath); WsMediaFile kodfile = new WsMediaFile { roomurl = mStaticThings.I.nowRoomServerUrl, preurl = mStaticThings.I.ThisKODfileUrl, url = HttpUrl.Value, name = SceneName.Value, size = "11111", ext = "scene", mtime = sendfile.sign, isupdate = false, fileMd5 = "" }; WsSceneInfo newscene = new WsSceneInfo { id = SceneName.Value, scene = sendfile.path, name = SceneName.Value, version = Sign.Value, isremote = true, isupdate = false, iskod = true, icon = RoomIconUrl.Value, kod = kodfile, cryptAPI = CryptAPI.Value, ckind = CryptKind.Value }; MessageDispatcher.SendMessage(true, VrDispMessageType.LoadLocalPathScene.ToString(), newscene, 0); } else { Fsm.Event(GetLocalPathFaild); } } }
public Task Write(Stream stream, ILogger log, CancellationToken token) { _downloaded = true; _hasChanges = true; using (stream) { if (File.Exists(LocalCacheFile.FullName)) { LocalCacheFile.Delete(); } using (var writeStream = File.OpenWrite(LocalCacheFile.FullName)) { stream.CopyTo(writeStream); stream.Seek(0, SeekOrigin.Begin); } } return(Task.FromResult(true)); }
protected override async Task CopyFromSource(ILogger log, CancellationToken token) { Uri absoluteUri = UriUtility.GetPath(RootPath, key); if (!await FileExistsAsync(client, bucketName, key, token).ConfigureAwait(false)) { return; } log.LogInformation($"GET {absoluteUri}"); if (File.Exists(LocalCacheFile.FullName)) { LocalCacheFile.Delete(); } string contentEncoding; using (FileStream cache = File.OpenWrite(LocalCacheFile.FullName)) { contentEncoding = await DownloadFileAsync(client, bucketName, key, cache, token).ConfigureAwait(false); } if (contentEncoding?.Equals("gzip", StringComparison.OrdinalIgnoreCase) == true) { log.LogInformation($"Decompressing {absoluteUri}"); string gzipFile = LocalCacheFile.FullName + ".gz"; File.Move(LocalCacheFile.FullName, gzipFile); using (Stream destination = File.Create(LocalCacheFile.FullName)) using (Stream source = File.OpenRead(gzipFile)) using (Stream zipStream = new GZipStream(source, CompressionMode.Decompress)) { await zipStream.CopyToAsync(destination, DefaultCopyBufferSize, token).ConfigureAwait(false); } } }