Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="serverName"></param>
        public List <SiteDatasource> ExecuteRequest()
        {
            var downloadedContent = new List <SiteDatasource>();

            //Depending on the HTTP download file type we want different file extensions
            var typeMapper = new DownloadPayloadTypeHelper("tdsx", "tds");

            var datasources = Datasources;

            if (datasources == null)
            {
                Login.Logger.Error("NULL datasources. Aborting download.");
                return(null);
            }

            //For each datasource, download it and save it to the local file system
            foreach (var dsInfo in datasources)
            {
                //Local path save the workbook
                string urlDownload = Urls.Url_DatasourceDownload(Login, dsInfo);
                Login.Logger.Information("Starting Datasource download " + dsInfo.Name);
                try
                {
                    //Generate the directory name we want to download into
                    var pathToSaveTo = FileIOHelper.EnsureProjectBasedPath(
                        LocalSavePath,
                        DownloadToProjectDirectories,
                        dsInfo,
                        Login.Logger);

                    var fileDownloaded       = this.DownloadFile(urlDownload, pathToSaveTo, dsInfo.Name, typeMapper);
                    var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);
                    Login.Logger.Information("Finished Datasource download " + fileDownloadedNoPath);

                    //Add to the list of our downloaded data sources
                    if (!string.IsNullOrEmpty(fileDownloaded))
                    {
                        downloadedContent.Add(dsInfo);

                        //Generate the metadata file that has additional server provided information about the workbook
                        if (GenerateInfoFile)
                        {
                            Helpers.DatasourcePublishSettings.CreateSettingsFile(dsInfo, fileDownloaded, SiteUserLookup);
                        }
                    }
                    else
                    {
                        //We should never hit this code; just being defensive
                        Login.Logger.Error("Download error, no local file path for downloaded content");
                    }
                }
                catch (Exception ex)
                {
                    Login.Logger.Error("Error during Datasource download " + dsInfo.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
                }
            }

            //Return the set of successfully downloaded content
            return(downloadedContent);
        }
Example #2
0
    /// <summary>
    /// Run the command
    /// </summary>
    /// <returns></returns>
    public string ExecuteRequest()
    {
        var statusLog     = _onlineSession.StatusLog;
        var workbook      = _workbook;
        var saveDirectory = _localSavePathRoot;

        if ((workbook == null) || (string.IsNullOrWhiteSpace(workbook.Id)))
        {
            statusLog.AddError("1025-851: NULL workbook. Aborting download.");
            return(null);
        }

        //Local path save the workbook
        string urlDownload = _onlineUrls.Url_WorkbookThumbnailDownload(_onlineSession, workbook);

        statusLog.AddStatus("Starting Workbook thumbnail download " + workbook.Name + " " + workbook.ToString());
        try
        {
            var fileDownloaded = this.DownloadFile(
                urlDownload,
                saveDirectory,
                workbook.Id,
                DownloadPayloadTypeHelper.CreateForImageDownload());
            var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);

            statusLog.AddStatus("Finished Workbook thumbnail download " + fileDownloadedNoPath);
            return(fileDownloaded);
        }
        catch (Exception ex)
        {
            statusLog.AddError("1025-1035: Error during Workbook thumbnial download " + workbook.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
            return(null); //Thumbnail not downloaded
        }
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="serverName"></param>
    public ICollection <SiteWorkbook> ExecuteRequest()
    {
        var statusLog         = _onlineSession.StatusLog;
        var downloadedContent = new List <SiteWorkbook>();

        var workbooks = _workbooks;

        if (workbooks == null)
        {
            statusLog.AddError("NULL workbooks. Aborting download.");
            return(null);
        }

        //Depending on the HTTP download file type we want different file extensions
        var typeMapper = new DownloadPayloadTypeHelper("twbx", "twb");

        foreach (var contentInfo in workbooks)
        {
            //Local path save the workbook
            string urlDownload = _onlineUrls.Url_WorkbookDownload(_onlineSession, contentInfo);
            statusLog.AddStatus("Starting Workbook download " + contentInfo.Name + " " + contentInfo.ToString());
            try
            {
                //Generate the directory name we want to download into
                var pathToSaveTo = FileIOHelper.EnsureProjectBasedPath(
                    _localSavePath,
                    _downloadToProjectDirectories,
                    contentInfo,
                    this.StatusLog);

                var fileDownloaded       = this.DownloadFile(urlDownload, pathToSaveTo, contentInfo.Name, typeMapper);
                var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);
                statusLog.AddStatus("Finished Workbook download " + fileDownloadedNoPath);

                //Add to the list of our downloaded workbooks, and save metadata
                if (!string.IsNullOrWhiteSpace(fileDownloaded))
                {
                    downloadedContent.Add(contentInfo);

                    //Generate the metadata file that has additional server provided information about the workbook
                    if (_generateInfoFile)
                    {
                        WorkbookPublishSettings.CreateSettingsFile(contentInfo, fileDownloaded, _siteUserLookup);
                    }
                }
                else
                {
                    //We should never hit this code; just being defensive
                    statusLog.AddError("Download error, no local file path for downloaded content");
                }
            }
            catch (Exception ex)
            {
                statusLog.AddError("Error during Workbook download " + contentInfo.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
            }
        } //foreach

        return(downloadedContent);
    }
        /// <summary>
        /// Execute the REST API call for a list of data sources
        /// </summary>
        public List <SiteDatasource> ExecuteRequest()
        {
            var statusLog         = OnlineSession.StatusLog;
            var downloadedContent = new List <SiteDatasource>();

            //Depending on the HTTP download file type we want different file extensions
            var typeMapper = new DownloadPayloadTypeHelper("tdsx", "tds");

            var datasources = _datasources;

            if (datasources == null)
            {
                statusLog.AddError("NULL datasources. Aborting download.");
                return(null);
            }

            //For each datasource, download it and save it to the local file system
            foreach (var dsInfo in datasources)
            {
                //Local path save the workbook
                string urlDownload = _onlineUrls.Url_DatasourceDownload(OnlineSession, dsInfo);
                statusLog.AddStatus("Starting Datasource download " + dsInfo.Name);
                try
                {
                    //Generate the directory name we want to download into
                    var pathToSaveTo = FileIOHelper.EnsureProjectBasedPath(
                        _localSavePath,
                        _downloadToProjectDirectories,
                        dsInfo,
                        this.StatusLog);

                    var fileDownloaded       = this.DownloadFile(urlDownload, pathToSaveTo, dsInfo.Name, typeMapper);
                    var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);
                    statusLog.AddStatus("Finished Datasource download " + fileDownloadedNoPath);

                    //Add to the list of our downloaded data sources
                    if (!string.IsNullOrEmpty(fileDownloaded))
                    {
                        downloadedContent.Add(dsInfo);
                    }
                    else
                    {
                        //We should never hit this code; just being defensive
                        statusLog.AddError("Download error, no local file path for downloaded content");
                    }
                }
                catch (Exception ex)
                {
                    statusLog.AddError("Error during Datasource download " + dsInfo.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
                }
            } //foreach


            //Return the set of successfully downloaded content
            return(downloadedContent);
        }
Example #5
0
    /// <summary>
    /// Downloads a file
    /// </summary>
    /// <param name="urlDownload"></param>
    /// <param name="downloadToDirectory"></param>
    /// <param name="baseFileName">Filename without extension</param>
    /// <returns>The path to the downloaded file</returns>
    private string DownloadFile_inner(
        string urlDownload,
        string downloadToDirectory,
        string baseFilename,
        DownloadPayloadTypeHelper downloadTypeMapper,
        bool overwriteExistingFile = true)
    {
        //[2016-05-06] Interestingly 'GetFileNameWithoutExtension' does more than remove a ".xxxx" extension; it will also remove a preceding
        //            path (e.g. GetFileNameWithoutExtension('foo/bar.xxx') -> "bar'.  This is undesirable because these characters are valid
        //            in Tableau Server content names. Since this function is supposed to be called with a 'baseFilename' that DOES NOT have a .xxx
        //            extension, it is safe to remove this call
        //baseFilename =  FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename));

        //Strip off an extension if its there
        baseFilename = FileIOHelper.GenerateWindowsSafeFilename(baseFilename);


        var webClient = this.CreateLoggedInWebClient();

        using (webClient)
        {
            //Choose a temp file name to download to
            var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp");
            //If the temp file exists, delete it
            if (System.IO.File.Exists(starterName))
            {
                System.IO.File.Delete(starterName);
            }

            _onlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10);
            webClient.DownloadFile(urlDownload, starterName); //Download the file

            //Look up the correct file extension based on the content type downloaded
            var contentType   = webClient.ResponseHeaders["Content-Type"];
            var fileExtension = downloadTypeMapper.GetFileExtension(contentType);
            var finishName    = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension);

            //See if a preexisting file is there
            if (System.IO.File.Exists(finishName))
            {
                if (overwriteExistingFile)
                {
                    System.IO.File.Delete(finishName);
                }
                else
                {
                    throw new Exception("1025-1152: File exists already " + finishName);
                }
            }

            //Rename the downloaded file
            System.IO.File.Move(starterName, finishName);
            return(finishName);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="serverName"></param>
    public List<SiteDatasource> ExecuteRequest()
    {
        var statusLog = _onlineSession.StatusLog;
        var downloadedContent = new List<SiteDatasource>();

        //Depending on the HTTP download file type we want different file extensions
        var typeMapper = new DownloadPayloadTypeHelper("tdsx", "tds");

        var datasources = _datasources;
        if (datasources == null)
        {
            statusLog.AddError("NULL datasources. Aborting download.");
            return null;
        }

        //For each datasource, download it and save it to the local file system
        foreach (var dsInfo in datasources)
        {
            //Local path save the workbook
            string urlDownload = _onlineUrls.Url_DatasourceDownload(_onlineSession, dsInfo);
            statusLog.AddStatus("Starting Datasource download " + dsInfo.Name);
            try
            {
                //Generate the directory name we want to download into
                var pathToSaveTo = FileIOHelper.EnsureProjectBasedPath(
                    _localSavePath,
                    _downloadToProjectDirectories,
                    dsInfo,
                    this.StatusLog);

                var fileDownloaded = this.DownloadFile(urlDownload, pathToSaveTo, dsInfo.Name, typeMapper);
                var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);
                statusLog.AddStatus("Finished Datasource download " + fileDownloadedNoPath);

                //Add to the list of our downloaded data sources
                if(!string.IsNullOrEmpty(fileDownloaded))
                {
                    downloadedContent.Add(dsInfo);
                }
                else
                {
                    //We should never hit this code; just being defensive
                    statusLog.AddError("Download error, no local file path for downloaded content");
                }
            }
            catch(Exception ex)
            {
                statusLog.AddError("Error during Datasource download " + dsInfo.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
            }
        } //foreach


        //Return the set of successfully downloaded content
        return downloadedContent;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="serverName"></param>
    public ICollection<SiteWorkbook> ExecuteRequest()
    {
        var statusLog = _onlineSession.StatusLog;
        var downloadedContent = new List<SiteWorkbook>();

        var workbooks = _workbooks;
        if (workbooks == null)
        {
            statusLog.AddError("NULL workbooks. Aborting download.");
            return null;
        }

        //Depending on the HTTP download file type we want different file extensions
        var typeMapper = new DownloadPayloadTypeHelper("twbx", "twb");

        foreach (var contentInfo in workbooks)
        {
            //Local path save the workbook
            string urlDownload = _onlineUrls.Url_WorkbookDownload(_onlineSession, contentInfo);
            statusLog.AddStatus("Starting Workbook download " + contentInfo.Name + " " + contentInfo.ToString());
            try
            {
                //Generate the directory name we want to download into
                var pathToSaveTo = FileIOHelper.EnsureProjectBasedPath(
                    _localSavePath, 
                    _downloadToProjectDirectories, 
                    contentInfo, 
                    this.StatusLog);

                var fileDownloaded = this.DownloadFile(urlDownload, pathToSaveTo, contentInfo.Name, typeMapper);
                var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);
                statusLog.AddStatus("Finished Workbook download " + fileDownloadedNoPath);

                //Add to the list of our downloaded data sources
                if (!string.IsNullOrWhiteSpace(fileDownloaded))
                {
                    downloadedContent.Add(contentInfo);
                }
                else
                {
                    //We should never hit this code; just being defensive
                    statusLog.AddError("Download error, no local file path for downloaded content");
                }
            }
            catch (Exception ex)
            {
                statusLog.AddError("Error during Workbook download " + contentInfo.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
            }
        } //foreach

        return downloadedContent;
    }
        /// <summary>
        /// Execute request for Workbooks
        /// </summary>
        public ICollection <SiteWorkbook> ExecuteRequest()
        {
            var statusLog         = OnlineSession.StatusLog;
            var downloadedContent = new List <SiteWorkbook>();

            if (_workbook == null)
            {
                statusLog.AddError("NULL workbook. Aborting download.");
                return(null);
            }

            //Depending on the HTTP download file type we want different file extensions
            var typeMapper = new DownloadPayloadTypeHelper("twbx", "twb");

            //Local path save the workbook
            string urlDownload = _onlineUrls.Url_WorkbookDownload(OnlineSession, _workbook);

            statusLog.AddStatus("Starting Workbook download " + _workbook.Name + " " + _workbook.ToString());
            try
            {
                //Generate the directory name we want to download into
                var pathToSaveTo = FileIOHelper.EnsureProjectBasedPath(_localSavePath, _workbook, StatusLog);

                var fileDownloaded       = DownloadFile(urlDownload, pathToSaveTo, _workbook.Name, typeMapper);
                var fileDownloadedNoPath = System.IO.Path.GetFileName(fileDownloaded);
                statusLog.AddStatus("Finished Workbook download " + fileDownloadedNoPath);

                //Add to the list of our downloaded data sources
                if (!string.IsNullOrWhiteSpace(fileDownloaded))
                {
                    downloadedContent.Add(_workbook);
                }
                else
                {
                    //We should never hit this code; just being defensive
                    statusLog.AddError("Download error, no local file path for downloaded content");
                }
            }
            catch (Exception ex)
            {
                statusLog.AddError("Error during Workbook download " + _workbook.Name + "\r\n  " + urlDownload + "\r\n  " + ex.ToString());
            }

            return(downloadedContent);
        }
    /// <summary>
    /// Download a file
    /// </summary>
    /// <param name="urlDownload"></param>
    /// <param name="downloadToDirectory"></param>
    /// <param name="baseFilename"></param>
    /// <param name="downloadTypeMapper"></param>
    /// <returns>The path to the downloaded file</returns>
    protected string DownloadFile(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper)
    {
        //Lets keep track of how long it took
        var startDownload = DateTime.Now;
        string outputPath;
        try
        {
            outputPath =  DownloadFile_inner(urlDownload, downloadToDirectory, baseFilename, downloadTypeMapper);
        }
        catch (Exception exDownload)
        {
            this.StatusLog.AddError("Download failed after " + (DateTime.Now - startDownload).TotalSeconds.ToString("#.#") + " seconds. " + urlDownload);

            var failedDownload = DateTime.Now;
            throw exDownload;
        }

        var finishDownload = DateTime.Now;
        this.StatusLog.AddStatus("Download success duration " + (finishDownload - startDownload).TotalSeconds.ToString("#.#") + " seconds. " + urlDownload, -10);
        return outputPath;
    }
     /// <summary>
    /// Downloads a file
    /// </summary>
    /// <param name="urlDownload"></param>
    /// <param name="downloadToDirectory"></param>
    /// <param name="baseFileName">Filename without extension</param>
    /// <returns>The path to the downloaded file</returns>
    private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper)
    {

        //Strip off an extension if its there
        baseFilename =  FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename));

        var webClient = this.CreateLoggedInWebClient();
        using(webClient)
        { 
            //Choose a temp file name to download to
            var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp");
            _onlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10);
            webClient.DownloadFile(urlDownload, starterName); //Download the file

            //Look up the correct file extension based on the content type downloaded
            var contentType = webClient.ResponseHeaders["Content-Type"];
            var fileExtension = downloadTypeMapper.GetFileExtension(contentType);
            var finishName = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension);

            //Rename the downloaded file
            System.IO.File.Move(starterName, finishName);
            return finishName;
        }
    }
Example #11
0
        /// <summary>
        /// Download a file
        /// </summary>
        /// <param name="urlDownload"></param>
        /// <param name="downloadToDirectory"></param>
        /// <param name="baseFilename"></param>
        /// <param name="downloadTypeMapper"></param>
        /// <returns>The path to the downloaded file</returns>
        protected string DownloadFile(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper)
        {
            //Lets keep track of how long it took
            var    startDownload = DateTime.Now;
            string outputPath;

            try
            {
                outputPath = DownloadFile_inner(urlDownload, downloadToDirectory, baseFilename, downloadTypeMapper);
            }
            catch (Exception exDownload)
            {
                Login.Logger.Error("Download failed after " + (DateTime.Now - startDownload).TotalSeconds.ToString("#.#") + " seconds. " + urlDownload);
                throw exDownload;
            }

            var finishDownload = DateTime.Now;

            Login.Logger.Information("Download success duration " + (finishDownload - startDownload).TotalSeconds.ToString("#.#") + " seconds. " + urlDownload);
            return(outputPath);
        }
Example #12
0
        /// <summary>
        /// Downloads a file
        /// </summary>
        /// <param name="urlDownload"></param>
        /// <param name="downloadToDirectory"></param>
        /// <param name="baseFileName">Filename without extension</param>
        /// <returns>The path to the downloaded file</returns>
        private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper)
        {
            //Strip off an extension if its there
            baseFilename = FileIOHelper.GenerateWindowsSafeFilename(baseFilename);

            var webClient = CreateLoggedInWebClient();

            using (webClient)
            {
                //Choose a temp file name to download to
                var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp");
                Login.Logger.Information("Attempting file download: " + urlDownload);

                using (HttpResponseMessage response = webClient.GetAsync(urlDownload, HttpCompletionOption.ResponseHeadersRead).Result)
                {
                    response.EnsureSuccessStatusCode();

                    using (
                        Stream contentStream = response.Content.ReadAsStreamAsync().Result,
                        fileStream = new FileStream(starterName, FileMode.Create, FileAccess.Write, FileShare.None,
                                                    8192, true))
                    {
                        var buffer       = new byte[8192];
                        var isMoreToRead = true;

                        do
                        {
                            var read = contentStream.ReadAsync(buffer, 0, buffer.Length).Result;
                            if (read == 0)
                            {
                                isMoreToRead = false;
                            }
                            else
                            {
                                fileStream.WriteAsync(buffer, 0, read);
                            }
                        } while (isMoreToRead);
                    }

                    //Look up the correct file extension based on the content type downloaded
                    var contentType   = response.Content.Headers.ContentType.ToString();
                    var fileExtension = downloadTypeMapper.GetFileExtension(contentType);
                    var finishName    = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension);

                    //Rename the downloaded file
                    System.IO.File.Move(starterName, finishName);
                    return(finishName);
                }
            }
        }
    /// <summary>
    /// Downloads a file
    /// </summary>
    /// <param name="urlDownload"></param>
    /// <param name="downloadToDirectory"></param>
    /// <param name="baseFileName">Filename without extension</param>
    /// <returns>The path to the downloaded file</returns>
    private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper)
    {
        //[2016-05-06] Interestingly 'GetFileNameWithoutExtension' does more than remove a ".xxxx" extension; it will also remove a preceding
        //            path (e.g. GetFileNameWithoutExtension('foo/bar.xxx') -> "bar'.  This is undesirable because these characters are valid
        //            in Tableau Server content names. Since this function is supposed to be called with a 'baseFilename' that DOES NOT have a .xxx
        //            extension, it is safe to remove this call
        //baseFilename =  FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename));

        //Strip off an extension if its there
        baseFilename = FileIOHelper.GenerateWindowsSafeFilename(baseFilename);

        var webClient = this.CreateLoggedInWebClient();
        using(webClient)
        {
            //Choose a temp file name to download to
            var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp");
            _onlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10);
            webClient.DownloadFile(urlDownload, starterName); //Download the file

            //Look up the correct file extension based on the content type downloaded
            var contentType = webClient.ResponseHeaders["Content-Type"];
            var fileExtension = downloadTypeMapper.GetFileExtension(contentType);
            var finishName = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension);

            //Rename the downloaded file
            System.IO.File.Move(starterName, finishName);
            return finishName;
        }
    }
Example #14
0
    /// <summary>
    /// Downloads a file
    /// </summary>
    /// <param name="urlDownload"></param>
    /// <param name="downloadToDirectory"></param>
    /// <param name="baseFileName">Filename without extension</param>
    /// <returns>The path to the downloaded file</returns>
    private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper)
    {
        //Strip off an extension if its there
        baseFilename = FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename));

        var webClient = this.CreateLoggedInWebClient();

        using (webClient)
        {
            //Choose a temp file name to download to
            var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp");
            _onlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10);
            webClient.DownloadFile(urlDownload, starterName); //Download the file

            //Look up the correct file extension based on the content type downloaded
            var contentType   = webClient.ResponseHeaders["Content-Type"];
            var fileExtension = downloadTypeMapper.GetFileExtension(contentType);
            var finishName    = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension);

            //Rename the downloaded file
            System.IO.File.Move(starterName, finishName);
            return(finishName);
        }
    }