Exemplo n.º 1
0
 public ascx_Task_WebGet()
 {
     this.TaskType = "Web (GET request)";
     this.TaskName = "...";
     this.TaskFunction = () =>
     {
         var response = new Web().getUrlContents(TaskName);
         messageLabel.set_Text("response size: {0}".format(response.size()));
         return response != "";
     };
 }
        public static bool xmlDB_Libraries_ImportFromZip(this TM_Xml_Database tmDatabase, string zipFileToImport, string unzipPassword)
        {
            var result = false;
            try
            {
                var currentLibraryPath = tmDatabase.Path_XmlLibraries;
                if (currentLibraryPath.isNull())
                    return false;
                if (zipFileToImport.isUri())
                {
                    "[xmlDB_Libraries_ImportFromZip] provided value was an URL so, downloading it: {0}".info(zipFileToImport);
                    zipFileToImport = new Web().downloadBinaryFile(zipFileToImport);
                    //zipFileToImport =  zipFileToImport.uri().download();
                }
                "[xmlDB_Libraries_ImportFromZip] importing library from: {0}".info(zipFileToImport);
                if (zipFileToImport.fileExists().isFalse())
                    "[xmlDB_Libraries_ImportFromZip] could not find file to import".error(zipFileToImport);
                else
                {
                    // handle the zips we get from GitHub

                    var tempDir = @"..\_".add_RandomLetters(3).tempDir(false).fullPath(); //trying to make the unzip path as small as possible
                    var fastZip = new ICSharpCode.SharpZipLib.Zip.FastZip {Password = unzipPassword ?? ""};
                    fastZip.ExtractZip(zipFileToImport, tempDir, "");

                    Files.copyFolder(tempDir, currentLibraryPath, true, true,"");          // just copy all files into Library path
                    result = true;
                    /*
                    var gitZipFolderName = tempDir.folders().first().folderName();				// the first folder should be the one created by gitHub's zip
                    var xmlFile_Location1 = tempDir.pathCombine(gitZipFolderName + ".xml");
                    var xmlFile_Location2 = tempDir.pathCombine(gitZipFolderName).pathCombine(gitZipFolderName + ".xml");
                    if (xmlFile_Location1.fileExists() || xmlFile_Location2.fileExists())
                        // if these exists here, just copy the unziped files directly
                    {
                        Files.copyFolder(tempDir, currentLibraryPath, true, true, ".git");
                        if (xmlFile_Location1.fileExists())
                            Files.copy(xmlFile_Location1, currentLibraryPath.pathCombine(gitZipFolderName));
                        result = true;
                    }
                    else
                    {
                        //if (zipFileToImport.extension() == ".master")
                        var gitZipDir = tempDir.pathCombine(gitZipFolderName);
                        foreach (var libraryFolder in gitZipDir.folders())
                        {
                            var libraryName = libraryFolder.folderName();
                            var targetFolder = currentLibraryPath.pathCombine(libraryName);

                            //default behaviour is to override the existing libraries
                            Files.copyFolder(libraryFolder, currentLibraryPath);

                            //handle the case where the xml file is located outside the library folder
                            var libraryXmlFile = gitZipDir.pathCombine("{0}.xml".format(libraryName));
                            if (libraryXmlFile.fileExists())
                                Files.copy(libraryXmlFile, targetFolder);
                                    // put it in the Library folder which is where it really should be
                        }
                        var virtualMappings = gitZipDir.pathCombine("Virtual_Articles.xml");
                        if (virtualMappings.fileExists())
                        {
                            Files.copy(virtualMappings, currentLibraryPath); // copy virtual mappings if it exists
                            tmDatabase.mapVirtualArticles();
                        }
                        result = true;
                    } */
                }
            }
            catch (Exception ex)
            {
                ex.log("[xmlDB_Libraries_ImportFromZip]");
            }

            if (result)
                tmDatabase.reloadGuidanceExplorerObjects();
                //tmDatabase.loadLibraryDataFromDisk();

            return result;
        }
Exemplo n.º 3
0
        public static String tryToGetFileOrDirectoryFromDroppedObject(DragEventArgs dragEventArgs, bool downloadIfHttp)
        {
            var dataReceived = new List<object>();
            String[] sFormats = dragEventArgs.Data.GetFormats();
            foreach (string sFormat in sFormats)
                dataReceived.Add(dragEventArgs.Data.GetData(sFormat));

            foreach (object item in dataReceived)
            {
                if (item != null)
                    switch (item.GetType().Name)
                    {
                        case "String":
                            if (File.Exists(item.ToString()) || Directory.Exists(item.ToString()))
                                return item.ToString();
                            if ( item.ToString().ToLower().StartsWith("http"))
                            {
                                if (downloadIfHttp)
                                {
                                    var savedUrlContents = new Web().saveUrlContents(item.ToString());
                                    if (savedUrlContents != "" && File.Exists(savedUrlContents))
                                        return savedUrlContents;
                                }
                                return item.ToString();
                            }
                            break;
                        case "String[]":
                            foreach (string subItem in (string[]) item)
                                if (File.Exists(subItem) || Directory.Exists(subItem))
                                    return subItem;
                            break;
                    }
            }
            return "";
        }
		public void backup_Files(string backupFolder)
		{
			var files_BackupFolder = backupFolder.pathCombine("Files");
			files_BackupFolder.createDir();

			var files = WikiApi.allImages();
			"there are {0} files to download".info(files.size()); 
			Status_ProgressBar.maximum(files.size());
			Status_ProgressBar.value(0);
			foreach(var file in files)	
			{
				if (CancelBackup)
				{
					"In backup_Files, CancelBackup was set, so aborting backup".error();
					break;
				}
				var web = new Web();
				if (WikiApi.BasicAuth.valid())
 					web.Headers_Request.add("Authorization","Basic " + WikiApi.BasicAuth);
				web.downloadBinaryFile(file, files_BackupFolder);
				Status_ProgressBar.increment(1);
			}			

		}