2009-09-01 4 views
0

Pour l'un de mes projets, nous utilisons un réseau de diffusion de contenu (EdgeCast) et je me demandais si quelqu'un connaissait une bibliothèque existante (open source) pour C# que nous pourrions exploiter? En un mot, j'étudie la possibilité de créer des répertoires, de télécharger des fichiers et de permettre une maintenance générale (renommer des fichiers, supprimer des fichiers) via notre panneau d'administration. Toute idée serait appréciée.C# & ASP.NET - Quelqu'un crée-t-il une bibliothèque pour parler à EdgeCast CDN via FTP?


NOTE: mon code pour le faire. Je ne sais pas s'ils ont une API mise à jour à ce stade, alors vérifiez. Ce code est circa 2009 (tel quel, aucune garantie, utilisation à vos risques et périls, etc.).

EdgeCast.cs

using System; 
using edgeCastWS = [DOMAIN].com.edgecast.api.EdgeCastWebServices; 

namespace [DOMAIN].IO.ContentDeliveryNetwork 
{ 
    /// <summary> 
    /// <b>Authentication</b> 
    /// <para>Each web service call will include a string parameter called strCredential which will ensure that customers may only access data related to their own accounts. This credential is a string made up of a few separate pieces of information.</para> 
    /// <para>A typical credential a customer would use may look like: “c:bas:[email protected]:password”</para> 
    /// <para> * The field format of this string is delimited with a colon “:” character.</para> 
    /// <para> * The first field is the type of client which is calling the web service. “c” represents an individual customer.</para> 
    /// <para> * The second field is the security type. “bas” represents Basic Authentication.</para> 
    /// <para> * With basic authentication the third field is the username which is usually in email address notation.</para> 
    /// <para> * With basic authentication the fourth field is the password associated with the user.</para> 
    /// <para>If the username and password are not valid, then the credential will prevent the specified function from proceeding. A SOAP exception will be generated and sent back to the client that called the function. The message contained in the exception will contain the string “Access Denied.”</para> 
    /// </summary> 
    public class EdgeCast : IDisposable 
    { 
     #region Declarations 
     [ThreadStatic] 
     private static EdgeCast _current; 
     private static EdgeCastCredential _credential = null; 
     private static string _customerID = string.Empty; 
     private static edgeCastWS _edgeCastWS = null; 
     #endregion 

     #region Constructor 
     public EdgeCast() 
     { 
      if (_current != null) 
      { 
       throw new InvalidOperationException("Only one ContentDeliveryNetwork may be created on a given thread."); 
      } 
      _current = this; 
      _edgeCastWS = new edgeCastWS(); 
      ConstructCredentials(); 
      _customerID = AppSettings.EdgeCast_CustomerID; 
     } 
     #endregion 

     #region API Methods 
     /// <summary> 
     /// Purges a file from all of the EdgeCast caching servers. This process may take a few minutes to fully complete. 
     /// </summary> 
     /// <param name="Path">This is path of the file or folder to purge, in URL format. To specify a folder, please put a trailing slash at the end of the URL. All files under that folder will be purged</param> 
     /// <param name="MediaType">HTTP Large Object = 3, HTTP Small Object = 8, Flash = 2, Windows = 1</param> 
     /// <returns>An integer that indicates whether or not the transaction was successful. 0 represents yes, -1 represents no</returns> 
     internal static short PurgeFileFromEdge(string Path, int MediaType) 
     { 
      return _edgeCastWS.PurgeFileFromEdge(_credential.Credential, _customerID, Path, MediaType); 
     } 
     /// <summary> 
     /// Loads a file to disk on all of the EdgeCast caching servers. This process may take a few minutes to fully complete. 
     /// </summary> 
     /// <param name="Path">This is path of the file to load, in URL format. Folders may not be loaded and will be ignored</param> 
     /// <param name="MediaType">HTTP Large Object = 3, HTTP Small Object = 8</param> 
     /// <returns>An integer that indicates whether or not the transaction was successful. 0 represents yes, -1 represents no</returns> 
     internal static short LoadFileToEdge(string Path, int MediaType) 
     { 
      return _edgeCastWS.LoadFileToEdge(_credential.Credential, _customerID, Path, MediaType); 
     } 
     /// <summary> 
     /// This method call is used to add a token authentication directory. All additions and changes should be processed every 30 minutes 
     /// </summary> 
     /// <param name="Dir">Directory Path should be starting from the root. Example: /directory1/directory2</param> 
     /// <param name="MediaType">Use 1 for Windows, 2 for Flash, 3 for HTTP Caching/Progressive download.</param> 
     /// <returns></returns> 
     internal static short TokenDirAdd(string Dir, int MediaType) 
     { 
      return _edgeCastWS.TokenDirAdd(_credential.Credential, _customerID, Dir, MediaType); 
     } 
     internal static string TokenEncrypt(string Key, string Args) 
     { 
      return _edgeCastWS.TokenEncrypt(_credential.Credential, Key, Args); 
     } 
     internal static short TokenKeyUpdate(string Key, int MediaType) 
     { 
      return _edgeCastWS.TokenKeyUpdate(_credential.Credential, _customerID, Key, MediaType); 
     } 
     #endregion 

     #region FTP Methods 
     /// <summary> 
     /// Does a FTP Upload of a file on the local system 
     /// </summary> 
     /// <param name="targetPath">The full path to save to on the FTP server (.\Campaigns\Prod\[Company GUID])</param> 
     /// <param name="sourceFileName">The full path and filename of the file to be uploaded (C:\[DOMAIN]\files\[filename])</param> 
     internal static void FTP_UploadFile(string targetPath, string sourceFileName) 
     { 
      string[] dirs = targetPath.Split(new char[] { '\\', '/' }); 

      using (FTPFactory myFTP = new FTPFactory()) 
      { 
       myFTP.login(); 
       myFTP.setBinaryMode(true); 

       // Make sure the directories are there and change down to the bottom most directory... 
       foreach (string dirName in dirs) 
       { 
        bool dirExists = true; 
        try { myFTP.chdir(dirName); } 
        catch { dirExists = false; } 
        if (!dirExists) 
        { 
         myFTP.mkdir(dirName); 
         myFTP.chdir(dirName); 
        } 
       } 

       // upload the file now... 
       myFTP.upload(sourceFileName); 
      } 
     } 
     /// <summary> 
     /// Returns a string array of files in the target directory 
     /// </summary> 
     /// <param name="targetPath">The target directory</param> 
     /// <returns>string array or null if no files found</returns> 
     internal static string[] FTP_GetFileList(string targetPath) 
     { 
      string[] dirs = targetPath.Split(new char[] { '\\', '/' }); 
      string[] files = null; 

      using (FTPFactory myFTP = new FTPFactory()) 
      { 
       myFTP.login(); 
       myFTP.setBinaryMode(true); 

       // Make sure the directories are there and change down to the bottom most directory... 
       foreach (string dirName in dirs) 
       { 
        bool dirExists = true; 
        try { myFTP.chdir(dirName); } 
        catch { dirExists = false; } 
        if (!dirExists) 
        { 
         myFTP.mkdir(dirName); 
         myFTP.chdir(dirName); 
        } 
       } 

       // get the list of files now... 
       files = myFTP.getFileList("*.*"); 
      } 
      return files; 
     } 
     /// <summary> 
     /// Checks to see if a file exists 
     /// </summary> 
     /// <param name="targetPath">The CDN directory path to look in</param> 
     /// <param name="targetFile">The file to look for</param> 
     /// <returns>true = found, false = not found</returns> 
     internal static bool FTP_DoesFileExist(string targetPath, string targetFile) 
     { 
      bool retFlag = true; 
      string[] dirs = targetPath.Split(new char[] { '\\', '/' }); 

      using (FTPFactory myFTP = new FTPFactory()) 
      { 
       myFTP.login(); 
       myFTP.setBinaryMode(true); 

       // change to the target directory - if it does not exists, neither does the file! simple... 
       foreach (string dirName in dirs) 
       { 
        try { myFTP.chdir(dirName); } 
        catch { retFlag = false; } 
       } 
       if (retFlag) 
       { 
        try { retFlag = myFTP.getFileSize(targetFile) > 0; } 
        catch { retFlag = false; } 
       } 
      } 
      return retFlag; 
     } 
     internal static bool FTP_DoesFileExist(string targetFile) 
     { 
      string targetPath = targetFile.Replace(@"http://", ""); 
      targetPath = targetPath.Replace(@"https://", ""); 
      targetPath = targetPath.Replace(@"cdn1.[DOMAIN].com/", ""); 
      targetPath = targetPath.Replace(@"/", @"\"); 

      targetPath = targetPath.Substring(0, targetPath.LastIndexOf(@"\")); 
      targetFile = targetFile.Substring(targetFile.LastIndexOf(@"/") + 1); 
      return FTP_DoesFileExist(targetPath, targetFile); 
     } 
     #endregion 

     #region Helper Methods 
     static private string ConstructCredentials() 
     { 
      _credential = new EdgeCastCredential(); 
      return _credential.Credential; 
     } 
     static private string ConstructCredentials(string credentials) 
     { 
      _credential = new EdgeCastCredential(credentials); 
      return _credential.Credential; 
     } 
     static private string ConstructCredentials(string typeOfClient, string securityType, string userName, string password) 
     { 
      _credential = new EdgeCastCredential(typeOfClient, securityType, userName, password); 
      return _credential.Credential; 
     } 
     #endregion 

     #region IDisposable Members 
     public void Dispose() 
     { 
      _current = null; 
     } 
     #endregion 
    } 
} 

EdgeCastCredential.cs

using System; 
using rivSecurity = [DOMAIN].Security.Cryptography.Internal; 

namespace [DOMAIN].IO.ContentDeliveryNetwork 
{ 
    internal class EdgeCastCredential 
    { 
     #region Properties 
     public string TypeOfClient; 
     public string SecurityType; 
     public string UserName; 
     public string Password; 
     public string Credential { get { return String.Format("{0}:{1}:{2}:{3}", TypeOfClient, SecurityType, UserName, Password); } } 
     #endregion 

     #region Constructor 
     public EdgeCastCredential() 
     { 
      TypeOfClient = AppSettings.EdgeCast_TypeOfClient; 
      SecurityType = AppSettings.EdgeCast_SecurityType; 
      UserName = rivSecurity.Decrypt(AppSettings.EdgeCast_UserName); 
      Password = rivSecurity.Decrypt(AppSettings.EdgeCast_Password); 
     } 
     public EdgeCastCredential(string credentials) 
     { 
      string[] parts = credentials.Split(new char[] { ':' }); 
     } 
     public EdgeCastCredential(string typeOfClient, string securityType, string userName, string password) 
     { 
      TypeOfClient = typeOfClient; 
      SecurityType = securityType; 
      UserName = userName; 
      Password = password; 
     } 
     #endregion 
    } 
} 

Répondre

0

Peu importe. J'ai créé une bibliothèque FTP et utilisé le peu qu'ils avaient pour une API. Je peux pas maintenant lire/télécharger/supprimer des fichiers et purger le CDN.

+2

Voulez-vous dire "je peux maintenant"? –

+0

@Keith, pouvez-vous partager votre bibliothèque? Merci. –