public async Task <string> DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string path = uploadContext.GetContentsLocalFilePath(); PostNewImageResult result = new PostNewImageResult(); result.editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null); result.editEntryUri = uploadContext.Settings.GetString(EDIT_MEDIA_ENTRY_LINK, null); result.etag = uploadContext.Settings.GetString(MEDIA_ETAG, null); if (string.IsNullOrEmpty(result.editUri)) { await PostNewImage(path, false, result); } else { try { await UpdateImage(result.editUri, path, result.editEntryUri, result.etag, true, result); } catch (Exception e) { //Debug.Fail(e.ToString()); bool success = false; result.srcUrl = null; // compiler complains without this line try { // couldn't update existing image? try posting a new one await PostNewImage(path, false, result); success = true; if (e is WebException) { //Debug.WriteLine("Image PUT failed, but POST succeeded. PUT exception follows."); HttpRequestHelper.LogException((WebException)e); } } catch { } if (!success) { throw; // rethrow the exception from the update, not the post } } } uploadContext.Settings.SetString(EDIT_MEDIA_LINK, result.editUri); uploadContext.Settings.SetString(EDIT_MEDIA_ENTRY_LINK, result.editEntryUri); uploadContext.Settings.SetString(MEDIA_ETAG, null); await UpdateETag(uploadContext, result.editUri); return(result.srcUrl); }
public override string DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string albumName = ApplicationEnvironment.ProductName; string path = uploadContext.GetContentsLocalFilePath(); if (Options.FileUploadNameFormat != null && Options.FileUploadNameFormat.Length > 0) { string formattedFileName = uploadContext.FormatFileName(uploadContext.PreferredFileName); string[] chunks = StringHelper.Reverse(formattedFileName).Split(new char[] { '/' }, 2); if (chunks.Length == 2) albumName = StringHelper.Reverse(chunks[1]); } string EDIT_MEDIA_LINK = "EditMediaLink"; string srcUrl; string editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null); if (editUri == null || editUri.Length == 0) { PostNewImage(albumName, path, out srcUrl, out editUri); } else { try { UpdateImage(editUri, path, out srcUrl, out editUri); } catch (Exception e) { Trace.Fail(e.ToString()); if (e is WebException) HttpRequestHelper.LogException((WebException)e); bool success = false; srcUrl = null; // compiler complains without this line try { // couldn't update existing image? try posting a new one PostNewImage(albumName, path, out srcUrl, out editUri); success = true; } catch { } if (!success) throw; // rethrow the exception from the update, not the post } } uploadContext.Settings.SetString(EDIT_MEDIA_LINK, editUri); PicasaRefererBlockingWorkaround(uploadContext.BlogId, uploadContext.Role, ref srcUrl); return srcUrl; }
public string DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string path = uploadContext.GetContentsLocalFilePath(); string srcUrl; string editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null); string editEntryUri = uploadContext.Settings.GetString(EDIT_MEDIA_ENTRY_LINK, null); string etag = uploadContext.Settings.GetString(MEDIA_ETAG, null); if (string.IsNullOrEmpty(editUri)) { PostNewImage(path, false, out srcUrl, out editUri, out editEntryUri); } else { try { UpdateImage(ref editUri, path, editEntryUri, etag, true, out srcUrl); } catch (Exception e) { Trace.Fail(e.ToString()); bool success = false; srcUrl = null; // compiler complains without this line try { // couldn't update existing image? try posting a new one PostNewImage(path, false, out srcUrl, out editUri, out editEntryUri); success = true; if (e is WebException) { Trace.WriteLine("Image PUT failed, but POST succeeded. PUT exception follows."); HttpRequestHelper.LogException((WebException)e); } } catch { } if (!success) { throw; // rethrow the exception from the update, not the post } } } uploadContext.Settings.SetString(EDIT_MEDIA_LINK, editUri); uploadContext.Settings.SetString(EDIT_MEDIA_ENTRY_LINK, editEntryUri); uploadContext.Settings.SetString(MEDIA_ETAG, null); UpdateETag(uploadContext, editUri); return(srcUrl); }
public string DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string path = uploadContext.GetContentsLocalFilePath(); string srcUrl; string editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null); string editEntryUri = uploadContext.Settings.GetString(EDIT_MEDIA_ENTRY_LINK, null); string etag = uploadContext.Settings.GetString(MEDIA_ETAG, null); if (string.IsNullOrEmpty(editUri)) { PostNewImage(path, false, out srcUrl, out editUri, out editEntryUri); } else { try { UpdateImage(ref editUri, path, editEntryUri, etag, true, out srcUrl); } catch (Exception e) { Trace.Fail(e.ToString()); bool success = false; srcUrl = null; // compiler complains without this line try { // couldn't update existing image? try posting a new one PostNewImage(path, false, out srcUrl, out editUri, out editEntryUri); success = true; if (e is WebException) { Trace.WriteLine("Image PUT failed, but POST succeeded. PUT exception follows."); HttpRequestHelper.LogException((WebException)e); } } catch { } if (!success) throw; // rethrow the exception from the update, not the post } } uploadContext.Settings.SetString(EDIT_MEDIA_LINK, editUri); uploadContext.Settings.SetString(EDIT_MEDIA_ENTRY_LINK, editEntryUri); uploadContext.Settings.SetString(MEDIA_ETAG, null); UpdateETag(uploadContext, editUri); return srcUrl; }
public string DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string albumName = ApplicationEnvironment.ProductName; string path = uploadContext.GetContentsLocalFilePath(); if (Options.FileUploadNameFormat != null && Options.FileUploadNameFormat.Length > 0) { string formattedFileName = uploadContext.FormatFileName(uploadContext.PreferredFileName); string[] chunks = StringHelper.Reverse(formattedFileName).Split(new char[] { '/' }, 2); if (chunks.Length == 2) { albumName = StringHelper.Reverse(chunks[1]); } } return(PostNewImage(albumName, path)); }
public override void DoUploadWorkAfterPublish(IFileUploadContext uploadContext) { try { _blogClient.DoAfterPublishUploadWork(uploadContext); } catch (IOException ex) { Trace.Fail(ex.ToString()); throw new BlogClientIOException(new FileInfo(uploadContext.GetContentsLocalFilePath()), ex); } catch (BlogClientException ex) { Trace.Fail(ex.ToString()); throw; } catch (Exception ex) { Trace.Fail(ex.ToString()); throw new BlogClientException(Res.Get(StringId.FileUploadFailedException), ex.Message); } return; }
public string DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string albumName = ApplicationEnvironment.ProductName; string path = uploadContext.GetContentsLocalFilePath(); if (Options.FileUploadNameFormat != null && Options.FileUploadNameFormat.Length > 0) { string formattedFileName = uploadContext.FormatFileName(uploadContext.PreferredFileName); string[] chunks = StringHelper.Reverse(formattedFileName).Split(new char[] { '/' }, 2); if (chunks.Length == 2) { albumName = StringHelper.Reverse(chunks[1]); } } string EDIT_MEDIA_LINK = "EditMediaLink"; string srcUrl; string editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null); if (editUri == null || editUri.Length == 0) { PostNewImage(albumName, path, out srcUrl, out editUri); } else { try { UpdateImage(editUri, path, out srcUrl, out editUri); } catch (Exception e) { Trace.Fail(e.ToString()); if (e is WebException) { HttpRequestHelper.LogException((WebException)e); } bool success = false; srcUrl = null; // compiler complains without this line try { // couldn't update existing image? try posting a new one PostNewImage(albumName, path, out srcUrl, out editUri); success = true; } catch { } if (!success) { throw; // rethrow the exception from the update, not the post } } } uploadContext.Settings.SetString(EDIT_MEDIA_LINK, editUri); PicasaRefererBlockingWorkaround(uploadContext.BlogId, uploadContext.Role, ref srcUrl); return(srcUrl); }
public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext) { try { ConnectForUpload(); string uploadPath = uploadContext.Settings.GetString(UPLOAD_PATH, null); bool overwrite = uploadPath != null; if (uploadPath == null) { string uploadFolder = null; string fileName = uploadContext.PreferredFileName; string filePath = uploadContext.FormatFileName(fileName); string[] pathParts = filePath.Split('/'); if (pathParts.Length > 1) { uploadFolder = FileHelper.GetValidAnsiFileName(pathParts[0]); for (int i = 1; i < pathParts.Length - 1; i++) uploadFolder = uploadFolder + "/" + FileHelper.GetValidAnsiFileName(pathParts[i]); } fileName = FileHelper.GetValidAnsiFileName(pathParts[pathParts.Length - 1]); uploadPath = _fileDestination.CombinePath(uploadFolder, fileName); if (_fileDestination.FileExists(uploadPath)) { string fileBaseName = Path.GetFileNameWithoutExtension(fileName); string fileExtension = Path.GetExtension(fileName); try { Hashtable existingFiles = new Hashtable(); foreach (string name in _fileDestination.ListFiles(uploadFolder)) existingFiles[name] = name; for (int i = 3; i < Int32.MaxValue && existingFiles.ContainsKey(fileName); i++) { fileName = FileHelper.GetValidAnsiFileName(fileBaseName + "_" + i + fileExtension); } } catch (Exception e) { Debug.Fail("Error while calculating unique filename", e.ToString()); } uploadPath = _fileDestination.CombinePath(uploadFolder, fileName); if (_fileDestination.FileExists(uploadPath)) { Debug.Fail("Failed to calculate unique filename"); fileName = FileHelper.GetValidAnsiFileName(fileBaseName + Guid.NewGuid().ToString() + fileExtension); uploadPath = _fileDestination.CombinePath(uploadFolder, fileName); } } } // transfer the file _fileDestination.DoTransfer( uploadContext.GetContentsLocalFilePath(), uploadPath, overwrite); uploadContext.Settings.SetString(UPLOAD_PATH, uploadPath); // return the url to the transferred file string baseUrl = UrlHelper.InsureTrailingSlash(_settings.UrlMapping); string relativeUrl = uploadPath; return new Uri(UrlHelper.UrlCombine(baseUrl, relativeUrl)); } catch (Exception ex) { WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex); throw new BlogClientFileTransferException(new FileInfo(uploadContext.GetContentsLocalFilePath()), message.Title, message.Text); } }
public override void DoUploadWorkAfterPublish(IFileUploadContext uploadContext) { try { _blogClient.DoAfterPublishUploadWork(uploadContext); } catch (IOException ex) { Trace.Fail(ex.ToString()); throw new BlogClientIOException(new FileInfo(uploadContext.GetContentsLocalFilePath()), ex); } catch (BlogClientException ex) { Trace.Fail(ex.ToString()); throw; } catch (Exception ex) { Trace.Fail(ex.ToString()); throw new BlogClientException(Res.Get(StringId.FileUploadFailedException), ex.Message); } return; }
public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext) { try { string uploadUrl = _blogClient.DoBeforePublishUploadWork(uploadContext); if (!UrlHelper.IsUrl(uploadUrl)) { string baseURL; if (uploadUrl.StartsWith("/")) baseURL = UrlHelper.GetBaseUrl(_blogHomepageUrl); else baseURL = UrlHelper.GetBasePathUrl(_blogHomepageUrl); uploadUrl = UrlHelper.UrlCombineIfRelative(baseURL, uploadUrl); } return new Uri(uploadUrl); } catch (BlogClientOperationCancelledException) { throw; // No need to assert when an operation is cancelled } catch (IOException ex) { Trace.Fail(ex.ToString()); throw new BlogClientIOException(new FileInfo(uploadContext.GetContentsLocalFilePath()), ex); } catch (BlogClientAuthenticationException ex) { Trace.Fail(ex.ToString()); throw; } catch (BlogClientProviderException ex) { Trace.Fail(ex.ToString()); // provider exceptions that are not authentication exceptions are presumed // to be lack of support for newMediaObject we may want to filter this down // further -- not sure how to do this other than by trial and error with the // various services. throw new BlogClientFileUploadNotSupportedException(ex.ErrorCode, ex.ErrorString); } catch (BlogClientException ex) { Trace.Fail(ex.ToString()); throw; } catch (Exception ex) { Trace.Fail(ex.ToString()); if (ex is WebException) { HttpRequestHelper.LogException((WebException)ex); } throw new BlogClientException(Res.Get(StringId.FileUploadFailedException), ex.Message); } }
public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext) { try { ConnectForUpload(); string uploadPath = uploadContext.Settings.GetString(UPLOAD_PATH, null); bool overwrite = uploadPath != null; if (uploadPath == null) { string uploadFolder = null; string fileName = uploadContext.PreferredFileName; string filePath = uploadContext.FormatFileName(fileName); string[] pathParts = filePath.Split('/'); if (pathParts.Length > 1) { uploadFolder = FileHelper.GetValidAnsiFileName(pathParts[0]); for (int i = 1; i < pathParts.Length - 1; i++) { uploadFolder = uploadFolder + "/" + FileHelper.GetValidAnsiFileName(pathParts[i]); } } fileName = FileHelper.GetValidAnsiFileName(pathParts[pathParts.Length - 1]); uploadPath = _fileDestination.CombinePath(uploadFolder, fileName); if (_fileDestination.FileExists(uploadPath)) { string fileBaseName = Path.GetFileNameWithoutExtension(fileName); string fileExtension = Path.GetExtension(fileName); try { Hashtable existingFiles = new Hashtable(); foreach (string name in _fileDestination.ListFiles(uploadFolder)) { existingFiles[name] = name; } for (int i = 3; i < Int32.MaxValue && existingFiles.ContainsKey(fileName); i++) { fileName = FileHelper.GetValidAnsiFileName(fileBaseName + "_" + i + fileExtension); } } catch (Exception e) { Debug.Fail("Error while calculating unique filename", e.ToString()); } uploadPath = _fileDestination.CombinePath(uploadFolder, fileName); if (_fileDestination.FileExists(uploadPath)) { Debug.Fail("Failed to calculate unique filename"); fileName = FileHelper.GetValidAnsiFileName(fileBaseName + Guid.NewGuid().ToString() + fileExtension); uploadPath = _fileDestination.CombinePath(uploadFolder, fileName); } } } // transfer the file _fileDestination.DoTransfer( uploadContext.GetContentsLocalFilePath(), uploadPath, overwrite); uploadContext.Settings.SetString(UPLOAD_PATH, uploadPath); // return the url to the transferred file string baseUrl = UrlHelper.InsureTrailingSlash(_settings.UrlMapping); string relativeUrl = uploadPath; return(new Uri(UrlHelper.UrlCombine(baseUrl, relativeUrl))); } catch (Exception ex) { WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex); throw new BlogClientFileTransferException(new FileInfo(uploadContext.GetContentsLocalFilePath()), message.Title, message.Text); } }
public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext) { try { string uploadUrl = _blogClient.DoBeforePublishUploadWork(uploadContext); if (!UrlHelper.IsUrl(uploadUrl)) { string baseURL; if (uploadUrl.StartsWith("/")) { baseURL = UrlHelper.GetBaseUrl(_blogHomepageUrl); } else { baseURL = UrlHelper.GetBasePathUrl(_blogHomepageUrl); } uploadUrl = UrlHelper.UrlCombineIfRelative(baseURL, uploadUrl); } return(new Uri(uploadUrl)); } catch (BlogClientOperationCancelledException) { throw; // No need to assert when an operation is cancelled } catch (IOException ex) { Trace.Fail(ex.ToString()); throw new BlogClientIOException(new FileInfo(uploadContext.GetContentsLocalFilePath()), ex); } catch (BlogClientAuthenticationException ex) { Trace.Fail(ex.ToString()); throw; } catch (BlogClientProviderException ex) { Trace.Fail(ex.ToString()); // provider exceptions that are not authentication exceptions are presumed // to be lack of support for newMediaObject we may want to filter this down // further -- not sure how to do this other than by trial and error with the // various services. throw new BlogClientFileUploadNotSupportedException(ex.ErrorCode, ex.ErrorString); } catch (BlogClientException ex) { Trace.Fail(ex.ToString()); throw; } catch (Exception ex) { Trace.Fail(ex.ToString()); if (ex is WebException) { HttpRequestHelper.LogException((WebException)ex); } throw new BlogClientException(Res.Get(StringId.FileUploadFailedException), ex.Message); } }
public string DoBeforePublishUploadWork(IFileUploadContext uploadContext) { string path = uploadContext.GetContentsLocalFilePath(); return(DoPostImage(path)); }