예제 #1
0
        /// <summary>
        /// Add files using the multipart content-type
        /// </summary>
        /// <param name="fileName">The file name and extension</param>
        /// <param name="fileType">The file type</param>
        /// <param name="folderId">The id of the folder</param>
        /// <param name="description">The description of the file</param>
        /// <param name="source">The source of the original file</param>
        /// <param name="data">The data contained in the file being uploaded</param>
        /// <returns>Returns the file Id associated with the uploaded file</returns>
        public string AddLibraryFilesMultipart(string fileName, FileType fileType, string folderId, string description, FileSource source, byte[] data)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new IllegalArgumentException(ConstantContactClient.Resources.Errors.FileNameNull);
            }

            var extension = Path.GetExtension(fileName).ToLowerInvariant();

            string[] fileTypes = new string[5] {
                ".jpeg", ".jpg", ".gif", ".png", ".pdf"
            };

            if (!((IList <string>)fileTypes).Contains(extension))
            {
                throw new IllegalArgumentException(ConstantContactClient.Resources.Errors.FileTypeInvalid);
            }

            if (string.IsNullOrEmpty(folderId) || string.IsNullOrEmpty(description))
            {
                throw new IllegalArgumentException(ConstantContactClient.Resources.Errors.FieldNull);
            }

            if (data == null)
            {
                throw new IllegalArgumentException(ConstantContactClient.Resources.Errors.FileNull);
            }

            string result = null;
            string url    = String.Concat(Settings.Endpoints.Default.BaseUrl, Settings.Endpoints.Default.MyLibraryFiles);

            byte[]         content  = MultipartBuilder.CreateMultipartContent(fileName, data, null, fileType.ToString(), folderId, description, source.ToString());
            RawApiResponse response = RestClient.PostMultipart(url, UserServiceContext.AccessToken, UserServiceContext.ApiKey, content);

            if (response.IsError)
            {
                throw new ConstantContactClientException(response.GetErrorMessage());
            }

            if (!response.IsError && response.StatusCode == System.Net.HttpStatusCode.Accepted)
            {
                string location = response.Headers["Location"];
                int    idStart  = location.LastIndexOf("/") + 1;
                result = location.Substring(idStart);
            }

            return(result);
        }