public string GetMimetype(string filePath) { string mimetype; switch (FileIdentifier.IdentifyFile(filePath)) { case FileType.ASCII: mimetype = "text/plain"; break; case FileType.GZip: mimetype = "application/x-gzip"; break; case FileType.Tar: mimetype = "application/x-tar"; break; case FileType.TarGz: mimetype = "application/x-compressed-tar"; break; case FileType.Zip: mimetype = "application/zip"; break; default: mimetype = "application/octet-stream"; break; } return(mimetype); }
public string DownloadPackage(Uri url, string identifier, DateTime?updated) { _requestedURLs.Add(url); var cachedFile = _cache.GetCachedFilename(url, updated); if (!string.IsNullOrWhiteSpace(cachedFile)) { return(cachedFile); } else { var downloadedFile = Net.Download(url); string extension; switch (FileIdentifier.IdentifyFile(downloadedFile)) { case FileType.ASCII: extension = "txt"; break; case FileType.GZip: extension = "gz"; break; case FileType.Tar: extension = "tar"; break; case FileType.TarGz: extension = "tar.gz"; break; case FileType.Zip: extension = "zip"; string invalidReason; if (!NetFileCache.ZipValid(downloadedFile, out invalidReason)) { throw new Kraken($"{downloadedFile} is not a valid ZIP file: {invalidReason}"); } break; default: extension = "ckan-package"; break; } return(_cache.Store( url, downloadedFile, string.Format("netkan-{0}.{1}", identifier, extension), move: true )); } }
public string DownloadPackage(Uri url, string identifier) { EnsureNotDisposed(); var cachedFile = _cache.GetCachedFilename(url); if (!string.IsNullOrWhiteSpace(cachedFile)) { return(cachedFile); } else { var downloadedFile = Net.Download(url); string extension; switch (FileIdentifier.IdentifyFile(downloadedFile)) { case FileType.ASCII: extension = "txt"; break; case FileType.GZip: extension = "gz"; break; case FileType.Tar: extension = "tar"; break; case FileType.TarGz: extension = "tar.gz"; break; case FileType.Zip: extension = "zip"; break; default: extension = "ckan-package"; break; } return(_cache.Store( url, downloadedFile, string.Format("netkan-{0}.{1}", identifier, extension), move: true )); } }
private string DownloadPackage(Uri url, string identifier, DateTime?updated, Uri primaryUrl = null) { if (primaryUrl == null) { primaryUrl = url; } if (_overwriteCache && !_requestedURLs.Contains(url)) { // Discard cached file if command line says so, // but only the first time in each run _cache.Remove(url); } _requestedURLs.Add(url); var cachedFile = _cache.GetCachedFilename(primaryUrl, updated); if (!string.IsNullOrWhiteSpace(cachedFile)) { return(cachedFile); } else { var downloadedFile = Net.Download(url); string extension; switch (FileIdentifier.IdentifyFile(downloadedFile)) { case FileType.ASCII: extension = "txt"; break; case FileType.GZip: extension = "gz"; break; case FileType.Tar: extension = "tar"; break; case FileType.TarGz: extension = "tar.gz"; break; case FileType.Zip: extension = "zip"; string invalidReason; if (!NetFileCache.ZipValid(downloadedFile, out invalidReason)) { log.Debug($"{downloadedFile} is not a valid ZIP file: {invalidReason}"); throw new Kraken($"{url} is not a valid ZIP file: {invalidReason}"); } break; default: extension = "ckan-package"; break; } return(_cache.Store( primaryUrl, downloadedFile, string.Format("netkan-{0}.{1}", identifier, extension), move: true )); } }
internal static JObject HTTP(JObject orig_metadata, string remote_id, NetFileCache cache, IUser user) { var metadata = orig_metadata; // Check if we should auto-inflate. string kref = (string)metadata[expand_token]; if (kref == (string)orig_metadata[expand_token] || kref == "#/ckan/http") { log.InfoFormat("Inflating from HTTP download... {0}", metadata[expand_token]); metadata["download"] = remote_id; metadata["version"] = "0.0.0"; // add a dummy version that will be replaced by the KSP-AVC parser later metadata.Remove(expand_token); var remote_uri = new Uri(remote_id); var downloaded_file = Net.Download(remote_uri); var module = CkanModule.FromJson(metadata.ToString()); // Check the type of the downloaded file. FileType downloaded_file_type = FileIdentifier.IdentifyFile(downloaded_file); if (downloaded_file_type != FileType.Zip) { // We assume the downloaded file is a zip file later on. string error_message = String.Format("Downloaded file not identified as a zip. Got {0} instead.", downloaded_file_type.ToString()); throw new Kraken(error_message); } if (metadata[version_token] != null && (metadata[version_token].ToString()).StartsWith("#/ckan/ksp-avc")) { // TODO pass the correct vref here... var versionRemote = FindVersionRemote(metadata, metadata[version_token].ToString()); metadata.Remove(version_token); try { AVC avc = AVC.FromZipFile(module, downloaded_file, versionRemote.id); avc.InflateMetadata(metadata, null, null); metadata["version"] = avc.version.ToString(); module.version = avc.version; } catch (JsonReaderException) { user.RaiseMessage("Bad embedded KSP-AVC file for {0}, halting.", module); return(null); } // If we've done this, we need to re-inflate our mod, too. module = CkanModule.FromJson(metadata.ToString()); } else { throw new Kraken("No $vref specified, $kref HTTP method requires it, bailing out.."); } ModuleInstaller.CachedOrDownload(module.identifier, module.version, module.download, cache); } else { log.WarnFormat("Not inflating metadata for {0}", orig_metadata["identifier"]); } // metadata["download"] = metadata["download"].ToString() + '#' + metadata["version"].ToString(); return(metadata); }