Exemplo n.º 1
0
        public static void UploadDeleteFile(String oldUrl, String newUrl)
        {
            String oldFile = "";

            try
            {
                oldFile = HttpContext.Current.Server.MapPath(oldUrl);
            }
            catch { }

            String newFile = (!String.IsNullOrWhiteSpace(newUrl) ? HttpContext.Current.Server.MapPath(newUrl) : "");

            if (newFile == oldFile)
            {
                return;
            }

            if (!File.Exists(oldFile))
            {
                return;
            }

            // Eliminar o ficheiro antigo
            try
            {
                File.Delete(oldFile);
            }
            catch (Exception ex)
            {
                UtilsEx.Log(ex);
            }
        }
Exemplo n.º 2
0
        public static String CallWebService(String url, Dictionary <String, String> parms, String method)
        {
            Boolean isGet = (method.ToLowerInvariant() == "get");

            // Se for via POST, enviar dados
            if (!isGet)
            {
                NameValueCollection webParms = new NameValueCollection();
                if (parms != null)
                {
                    foreach (KeyValuePair <String, String> pair in parms)
                    {
                        webParms.Add(pair.Key, pair.Value);
                    }
                }

                try
                {
                    using (WebClient webClient = new WebClient())
                    {
                        byte[] bytes = webClient.UploadValues(url, method, webParms);
                        return(Encoding.UTF8.GetString(bytes));
                    }
                }
                catch (Exception ex)
                {
                    UtilsEx.Log(ex);
                    throw;
                }
            }

            // Se for GET, adicionar ao Url
            if (parms != null)
            {
                foreach (KeyValuePair <String, String> pair in parms)
                {
                    url += (url.IndexOf('?') > 0 ? "&" : "?") + pair.Key + "=" + Uri.EscapeDataString(pair.Value);
                }
            }

            try
            {
                using (WebClient webClient = new WebClient())
                {
                    byte[] bytes = webClient.DownloadData(url);
                    return(Encoding.UTF8.GetString(bytes));
                }
            }
            catch (Exception ex)
            {
                UtilsEx.Log(ex);
                throw;
            }
        }
Exemplo n.º 3
0
        public static void AtualizarBD()
        {
            try
            {
                if (String.IsNullOrEmpty(PathBackups))
                {
                    throw new ApplicationException("Caminho da pasta de backups em falta!");
                }

                if (String.IsNullOrEmpty(PathBackups))
                {
                    throw new ApplicationException("Caminho da pasta dos scripts em falta!");
                }

                if (!BackupBaseDados())
                {
                    throw new ApplicationException("Backup da base de dados nao foi feita!");
                }

                if (!HasAtualizacaoInDB())
                {
                    throw new ApplicationException("Não existe ou nao esta configurada a tabela de atualizacoes!");
                }



                BaseDados bd = new BaseDados();

                int VersaoRelease = -1;
                if (HasUpdates(out VersaoRelease))
                {
                    for (int i = VersaoRelease; i < (VersaoUpdate - VersaoRelease); i++)
                    {
                        UtilsEx.Log("Entrou no ciclo for. script n " + i);

                        string script = "\\" + string.Format(ScriptName, i);
                        string path   = PathScripts + script;

                        if (!File.Exists(path))
                        {
                            throw new ApplicationException("Script" + i + " não existe!");
                        }

                        try
                        {
                            string content = File.ReadAllText(path);
                            bd.ExecNonQuery(path);

                            UtilsEx.Log("Executou script n " + i);
                        }catch (Exception e)
                        {
                            UtilsEx.Log(e, "Erro ao executar: script" + i);
                            break;
                        }
                    }
                }
            }catch (Exception e)
            {
                UtilsEx.Log(e, "Erro ao fazer update da base de dados!");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Validar um ficheiro carregado e transformar em definitivo se necessário
        /// </summary>
        /// <returns>Devolve o URL para a localização definitiva do ficheiro</returns>
        public static String UploadCheckFile(String fileUrl, String savePath)
        {
            fileUrl = fileUrl.TrimStart('~', '/');
            fileUrl = "~/" + fileUrl;

            // Verificar se o ficheiro existe
            String filePath = HttpContext.Current.Server.MapPath(fileUrl);

            if (!File.Exists(filePath))
            {
                return("");
            }

            // Se já é um ficheiro definitivo, não altera
            String fileName = Path.GetFileName(filePath);

            if (!fileName.ToLowerInvariant().StartsWith("tmp_"))
            {
                return(fileUrl.TrimStart('~'));
            }

            // Se for um ficheiro temporário, transforma em definitivo
            String newPath = savePath.Trim('~', '/');

            newPath  = "~/" + newPath + "/";
            newPath += fileName.Substring(4);

            String newUrl = VirtualPathUtility.ToAbsolute(newPath);

            // Mover o ficheiro temporário para definitivo
            newPath = HttpContext.Current.Server.MapPath(newUrl);

            String fileDir = Path.GetDirectoryName(newPath);

            if (!Directory.Exists(fileDir))
            {
                try
                {
                    Directory.CreateDirectory(fileDir);
                }
                catch (Exception ex)
                {
                    UtilsEx.Log(ex);
                    return("");
                }
            }

            try
            {
                File.Move(filePath, newPath);
            }
            catch (Exception ex)
            {
                UtilsEx.Log(ex);
                return("");
            }

            newUrl = VirtualPathUtility.ToAppRelative(newUrl);
            newUrl = newUrl.TrimStart('~');

            return(newUrl);
        }