private void WriteUploadedFileCallback(IAsyncResult ar) { TransportClass3 oTransportClass3 = ar.AsyncState as TransportClass3; try { int nReadWriteBytes; ErrorTypes eError = oTransportClass3.m_oStorage.WriteFileEnd(ar, out nReadWriteBytes); if (ErrorTypes.NoError == eError) { oTransportClass3.m_oMediaXmlMapHash.Add(oTransportClass3.m_sHash, oTransportClass3.m_sFilename); oTransportClass3.m_oMediaXmlMapFilename.Add(oTransportClass3.m_sFilename, oTransportClass3.m_sHash); ImageUrlProcess(oTransportClass3, Constants.mc_sResourceServiceUrlRel + oTransportClass3.m_sPath.Replace('\\', '/')); } else { WriteToResponse(oTransportClass3, eError, null, oTransportClass3.m_aInputParams); } } catch (Exception e) { _log.Error("Exeption: ", e); WriteToResponse(oTransportClass3, ErrorTypes.Upload, null, oTransportClass3.m_aInputParams); } }
private void ReadContextCallback(IAsyncResult ar) { TransportClass2 oTransportClass2 = ar.AsyncState as TransportClass2; try { ErrorTypes eError = oTransportClass2.m_oAsyncContextReadOperation.ReadContextEnd(ar); if (ErrorTypes.NoError == eError) { HttpPostedFile oCurrentFile = ((HttpPostedFile)oTransportClass2.m_oFiles[(string)oTransportClass2.m_oFilesEnumerator.Current]); oCurrentFile.InputStream.Position = 0; Stream oImageStream = oCurrentFile.InputStream; byte[] aBuffer = oTransportClass2.m_oAsyncContextReadOperation.m_aOutput.ToArray(); int nImageFormat = FormatChecker.GetFileFormat(aBuffer); string sSupportedFormats = ConfigurationSettings.AppSettings["limits.image.types.upload"] ?? "jpg"; if (0 != (FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE & nImageFormat) && -1 != sSupportedFormats.IndexOf(FileFormats.ToString(nImageFormat))) { if (FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE_GIF == nImageFormat || FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE_ICO == nImageFormat) { byte[] aNewBuffer; if (Utils.ConvertGifIcoToPng(aBuffer, nImageFormat, out aNewBuffer)) { nImageFormat = FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE_PNG; aBuffer = aNewBuffer; oImageStream = new MemoryStream(aBuffer); } } string sImageHash = null; using (MemoryStream ms = new MemoryStream(aBuffer)) sImageHash = Utils.getMD5HexString(ms); string sFileName; if (oTransportClass2.m_oMediaXmlMapHash.TryGetValue(sImageHash, out sFileName)) { ImageUrlProcess(oTransportClass2, Constants.mc_sResourceServiceUrlRel + Path.Combine(oTransportClass2.m_sKey, @"media\" + sFileName).Replace('\\', '/')); } else { string sSearchName = "image"; List <int> aIndexes = new List <int>(); foreach (KeyValuePair <string, string> kvp in oTransportClass2.m_oMediaXmlMapFilename) { string sFilename = Path.GetFileNameWithoutExtension(kvp.Key); if (0 == sFilename.IndexOf(sSearchName)) { int nCurIndex; if (int.TryParse(sFilename.Substring(sSearchName.Length), out nCurIndex)) { aIndexes.Add(nCurIndex); } } } int nMaxIndex = -1; for (int i = 0, length = aIndexes.Count; i < length; ++i) { int nCurIndex = aIndexes[i]; if (nMaxIndex < nCurIndex) { nMaxIndex = nCurIndex; } } int nNewIndex = 1; if (nMaxIndex >= nNewIndex) { nNewIndex = nMaxIndex + 1; } string sNewName = sSearchName + nNewIndex + "." + FileFormats.ToString(nImageFormat); string sNewPath = Path.Combine(oTransportClass2.m_sKey, @"media\" + sNewName).Replace('\\', '/'); Storage oStorage = new Storage(); TransportClass3 oTransportClass3 = new TransportClass3(oTransportClass2, sNewName, sImageHash, sNewPath, oStorage); oTransportClass3.m_oStorage.WriteFileBegin(sNewPath, oImageStream, WriteUploadedFileCallback, oTransportClass3); } } else { WriteToResponse(oTransportClass2, ErrorTypes.UploadExtension, null, oTransportClass2.m_aInputParams); } } else { WriteToResponse(oTransportClass2, eError, null, oTransportClass2.m_aInputParams); } } catch (Exception e) { _log.Error("Exeption: ", e); WriteToResponse(oTransportClass2, ErrorTypes.Upload, null, oTransportClass2.m_aInputParams); } }
private void ReadContextCallback(IAsyncResult ar) { TransportClass2 oTransportClass2 = ar.AsyncState as TransportClass2; try { ErrorTypes eError = oTransportClass2.m_oAsyncContextReadOperation.ReadContextEnd(ar); if (ErrorTypes.NoError == eError) { HttpPostedFile oCurrentFile = ((HttpPostedFile)oTransportClass2.m_oFiles[(string)oTransportClass2.m_oFilesEnumerator.Current]); oCurrentFile.InputStream.Position = 0; Stream oImageStream = oCurrentFile.InputStream; byte[] aBuffer = oTransportClass2.m_oAsyncContextReadOperation.m_aOutput.ToArray(); int nImageFormat = FormatChecker.GetFileFormat(aBuffer); string sSupportedFormats = ConfigurationSettings.AppSettings["limits.image.types.upload"] ?? "jpg"; if (0 != (FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE & nImageFormat) && -1 != sSupportedFormats.IndexOf(FileFormats.ToString(nImageFormat))) { if (FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE_GIF == nImageFormat || FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE_ICO == nImageFormat) { byte[] aNewBuffer; if (Utils.ConvertGifIcoToPng(aBuffer, nImageFormat, out aNewBuffer)) { nImageFormat = FileFormats.AVS_OFFICESTUDIO_FILE_IMAGE_PNG; aBuffer = aNewBuffer; oImageStream = new MemoryStream(aBuffer); } } string sImageHash = null; using (MemoryStream ms = new MemoryStream(aBuffer)) sImageHash = Utils.getMD5HexString(ms); string sFileName; if (oTransportClass2.m_oMediaXmlMapHash.TryGetValue(sImageHash, out sFileName)) { ImageUrlProcess(oTransportClass2, Constants.mc_sResourceServiceUrlRel + Path.Combine(oTransportClass2.m_sKey, @"media\" + sFileName).Replace('\\', '/')); } else { string sSearchName = "image"; List<int> aIndexes = new List<int>(); foreach (KeyValuePair<string, string> kvp in oTransportClass2.m_oMediaXmlMapFilename) { string sFilename = Path.GetFileNameWithoutExtension(kvp.Key); if (0 == sFilename.IndexOf(sSearchName)) { int nCurIndex; if (int.TryParse(sFilename.Substring(sSearchName.Length), out nCurIndex)) aIndexes.Add(nCurIndex); } } int nMaxIndex = -1; for (int i = 0, length = aIndexes.Count; i < length; ++i) { int nCurIndex = aIndexes[i]; if (nMaxIndex < nCurIndex) nMaxIndex = nCurIndex; } int nNewIndex = 1; if (nMaxIndex >= nNewIndex) nNewIndex = nMaxIndex + 1; string sNewName = sSearchName + nNewIndex + "." + FileFormats.ToString(nImageFormat); string sNewPath = Path.Combine(oTransportClass2.m_sKey, @"media\" + sNewName).Replace('\\', '/'); Storage oStorage = new Storage(); TransportClass3 oTransportClass3 = new TransportClass3(oTransportClass2, sNewName, sImageHash, sNewPath, oStorage); oTransportClass3.m_oStorage.WriteFileBegin(sNewPath, oImageStream, WriteUploadedFileCallback, oTransportClass3); } } else WriteToResponse(oTransportClass2, ErrorTypes.UploadExtension, null, oTransportClass2.m_aInputParams); } else WriteToResponse(oTransportClass2, eError, null, oTransportClass2.m_aInputParams); } catch (Exception e) { _log.Error("Exeption: ", e); WriteToResponse(oTransportClass2, ErrorTypes.Upload, null, oTransportClass2.m_aInputParams); } }