Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new source file in your project.
        /// Do not call this function to create JSON files. You may only create a JSON manifest file using
        /// createNewAppsScriptManifestJSONFile()
        /// </summary>
        /// <param name="Name">The file name</param>
        /// <param name="Type">The file type you are adding</param>
        /// <param name="Sync">Sync current changes with new file to drive</param>
        public static Task <TaskInfo <string> > AddNewSourceFile(string Name, FILE_TYPES Type, bool Sync)
        {
            return(Task <TaskInfo <string> > .Factory.StartNew(() =>
            {
                try
                {
                    if (Sync)
                    {
                        if (libraryReady)
                        {
                            createFile(Name, Type);
                            uploadFiles();
                        }
                        return libraryUnitializedInfo;
                    }
                    else
                    {
                        createFile(Name, Type);
                    }

                    return new TaskInfo <string>("Source File Creation Complete");
                }
                catch (Exception ex)
                {
                    return new TaskInfo <string>(getExceptionString(ex, "Unable to create source code file."));
                }
            }
                                                               ));
        }
        private static void createFile(string name, FILE_TYPES extension)
        {
            string defaultCode = string.Empty;

            switch (extension)
            {
            case FILE_TYPES.SERVER_JS:
                defaultCode = "function myFunction() {" + Environment.NewLine + Environment.NewLine + "}";
                break;

            case FILE_TYPES.HTML:
                defaultCode = "<!DOCTYPE html>" + Environment.NewLine + "<html>" + Environment.NewLine + "  <head>" + Environment.NewLine + "    <base target=\"_top\">" + Environment.NewLine + "  </head>" + Environment.NewLine + "  <body>" + Environment.NewLine + Environment.NewLine + "  </body>" + Environment.NewLine + "</html>";
                break;

            default:
                Debug.WriteLine("Unable to create JSON file type with this function.");
                return;
            }

            string folder = getFolderFromFileType(extension).FullName;
            string ext    = getExtensionFromFileType(extension);

            string path = folder + "\\" + name + ext;

            if (!System.IO.File.Exists(path))
            {
                createFileAndWrite(path, defaultCode);
                Debug.WriteLine("Created File... Please synchronize changes when ready to upload.");
            }
            else
            {
                Debug.WriteLine("A file with that name already exists!");
            }
        }
        private static string getGoogleFileType(FILE_TYPES f)
        {
            switch (f)
            {
            case FILE_TYPES.SERVER_JS:
                return("SERVER_JS");

            case FILE_TYPES.HTML:
                return("HTML");

            default:
                return("JSON");
            }
        }
        private static DirectoryInfo getFolderFromFileType(FILE_TYPES f)
        {
            switch (f)
            {
            case FILE_TYPES.SERVER_JS:
                return(javascriptDirectory);

            case FILE_TYPES.HTML:
                return(htmlDirectory);

            case FILE_TYPES.JSON:
                return(jsonDirectory);
            }
            return(null);
        }
        private static string getExtensionFromFileType(FILE_TYPES f)
        {
            switch (f)
            {
            case FILE_TYPES.SERVER_JS:
                return(".js");

            case FILE_TYPES.HTML:
                return(".html");

            case FILE_TYPES.JSON:
                return(".json");

            default:
                return("");
            }
        }
        private static string GetType(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(DEFAULT_MIME_TYPE);
            }

            string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl(filePath));

            if (mimeType.NullOrEmpty())
            {
                if (!FILE_TYPES.TryGetValue(Path.GetExtension(filePath), out mimeType))
                {
                    return(DEFAULT_MIME_TYPE);
                }
            }

            return(mimeType);
        }