Ejemplo n.º 1
0
        private async Task <string> TryGetFileFromServerAsync(string urlForServer, string fileIndexPath, string cache)
        {
            string fullDestPath = Path.Combine(cache, fileIndexPath);

            Debug.Assert(!string.IsNullOrWhiteSpace(cache));
            if (String.IsNullOrWhiteSpace(urlForServer))
            {
                return(null);
            }

            // There are three ways symbol files can be indexed.  Start looking for each one.

            // First, check for the compressed location.  This is the one we really want to download.
            string compressedFilePath   = fileIndexPath.Substring(0, fileIndexPath.Length - 1) + "_";
            string compressedFileTarget = Path.Combine(cache, compressedFilePath);

            TryDeleteFile(compressedFileTarget);
            Task <string> compressedFilePathDownload = GetPhysicalFileFromServerAsync(urlForServer, compressedFilePath, compressedFileTarget);

            // Second, check if the raw file itself is indexed, uncompressed.
            Task <string> rawFileDownload = GetPhysicalFileFromServerAsync(urlForServer, fileIndexPath, fullDestPath);

            // Last, check for a redirection link.
            string        filePtrSigPath  = Path.Combine(Path.GetDirectoryName(fileIndexPath), "file.ptr");
            Task <string> filePtrDownload = GetPhysicalFileFromServerAsync(urlForServer, filePtrSigPath, fullDestPath, returnContents: true);


            // Handle compressed download.
            string result = await compressedFilePathDownload;

            if (result != null)
            {
                try
                {
                    // Decompress it
                    Command.Run("Expand " + Command.Quote(result) + " " + Command.Quote(fullDestPath));
                    Trace($"Found '{Path.GetFileName(fileIndexPath)}' on server '{urlForServer}'.  Copied to '{fullDestPath}'.");
                    return(fullDestPath);
                }
                catch (Exception e)
                {
                    Trace("Exception encountered while expanding file '{0}': {1}", result, e.Message);
                }
                finally
                {
                    if (File.Exists(result))
                    {
                        File.Delete(result);
                    }
                }
            }

            // Handle uncompressed download.
            result = await rawFileDownload;
            if (result != null)
            {
                Trace($"Found '{Path.GetFileName(fileIndexPath)}' on server '{urlForServer}'.  Copied to '{result}'.");
                return(result);
            }

            // Handle redirection case.
            var filePtrData = (await filePtrDownload ?? "").Trim();

            if (filePtrData.StartsWith("PATH:"))
            {
                filePtrData = filePtrData.Substring(5);
            }

            if (!filePtrData.StartsWith("MSG:") && File.Exists(filePtrData))
            {
                try
                {
                    using (FileStream input = File.OpenRead(filePtrData))
                        await CopyStreamToFileAsync(input, filePtrSigPath, fullDestPath, input.Length);

                    Trace($"Found '{Path.GetFileName(fileIndexPath)}' on server '{urlForServer}'.  Copied to '{fullDestPath}'.");
                    return(fullDestPath);
                }
                catch (Exception)
                {
                    Trace("Error copying from file.ptr: content '{0}' from '{1}' to '{2}'.", filePtrData, filePtrSigPath, fullDestPath);
                }
            }
            else if (!string.IsNullOrWhiteSpace(filePtrData))
            {
                Trace("Error resolving file.ptr: content '{0}' from '{1}'.", filePtrData, filePtrSigPath);
            }

            Trace($"No file matching '{Path.GetFileName(fileIndexPath)}' found on server '{urlForServer}'.");
            return(null);
        }
Ejemplo n.º 2
0
        private string TryGetFileFromServer(string urlForServer, string fileIndexPath, string cache)
        {
            Debug.Assert(!string.IsNullOrEmpty(cache));
            if (string.IsNullOrEmpty(urlForServer))
            {
                return(null);
            }

            string targetPath = Path.Combine(cache, fileIndexPath);

            // See if it is a compressed file by replacing the last character of the name with an _
            string compressedSigPath  = fileIndexPath.Substring(0, fileIndexPath.Length - 1) + "_";
            string compressedFilePath = GetPhysicalFileFromServer(urlForServer, compressedSigPath, cache);

            if (compressedFilePath != null)
            {
                try
                {
                    // Decompress it
                    Command.Run("Expand " + Command.Quote(compressedFilePath) + " " + Command.Quote(targetPath));
                    return(targetPath);
                }
                catch (Exception e)
                {
                    Trace("Exception encountered while expanding file '{0}': {1}", compressedFilePath, e.Message);
                }
                finally
                {
                    if (File.Exists(compressedFilePath))
                    {
                        File.Delete(compressedFilePath);
                    }
                }
            }

            // Just try to fetch the file directly
            string ret = GetPhysicalFileFromServer(urlForServer, fileIndexPath, cache);

            if (ret != null)
            {
                return(ret);
            }

            // See if we have a file that tells us to redirect elsewhere.
            string filePtrSigPath = Path.Combine(Path.GetDirectoryName(fileIndexPath), "file.ptr");
            string filePtrData    = GetPhysicalFileFromServer(urlForServer, filePtrSigPath, cache, true);

            if (filePtrData == null)
            {
                return(null);
            }

            filePtrData = filePtrData.Trim();
            if (filePtrData.StartsWith("PATH:"))
            {
                filePtrData = filePtrData.Substring(5);
            }

            if (!filePtrData.StartsWith("MSG:") && File.Exists(filePtrData))
            {
                using (FileStream fs = File.OpenRead(filePtrData))
                {
                    CopyStreamToFile(fs, filePtrData, targetPath, fs.Length);
                    return(targetPath);
                }
            }

            Trace("Error resolving file.ptr: content '{0}' from '{1}.", filePtrData, filePtrSigPath);

            return(null);
        }