Пример #1
0
        private static bool isSupported(string extension)
        {
            FileService.FileServiceClient client = new FileService.FileServiceClient();
            bool b = client.IsSupportedExtension(extension);

            client.Close();
            return(b);
        }
Пример #2
0
        private static void ExecuteTask()
        {
            Console.WriteLine("Progress: Execution started.");
            string lockObj         = "";
            string previousLockObj = "";

            using (WebClient client = new WebClient())
            {
                try
                {
                    if (string.IsNullOrEmpty(UserName))
                    {
                        client.UseDefaultCredentials = true;
                    }
                    else
                    {
                        client.UseDefaultCredentials = false;
                        client.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(UserName + ":" + Password)));
                    }
                    string metadataString = "";
                    lockObj = CreateLockObject(Id, "GetMetatadata");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        metadataString = GetCachedDataFromLockObject(lockObj);
                        if (metadataString == null)
                        {
                            metadataString = client.DownloadString(Repo + ODATA_SERVICE_PATH + string.Format(GET_CONTENT_BY_ID_REQUEST_FORMAT, Id));
                            SaveCacheDataInLockObject(lockObj, metadataString);
                        }
                        else
                        {
                            DeleteCachedDataInLockObject(lockObj);
                        }
                        previousLockObj = lockObj;
                        ReleaseLock(lockObj);
                    }

                    JsonSerializerSettings settings = new JsonSerializerSettings {
                        DateFormatHandling = DateFormatHandling.IsoDateFormat
                    };
                    JsonSerializer serializer = JsonSerializer.Create(settings);
                    StringReader   reader     = new StringReader(metadataString);
                    //DeleteCachedDataInLockObject(lockObj);
                    JsonTextReader jreader  = new JsonTextReader(reader);
                    StringBuilder  builder  = new StringBuilder();
                    StringWriter   writer   = new StringWriter(builder);
                    JsonTextWriter jwriter  = new JsonTextWriter(writer);
                    JObject        metadata = serializer.Deserialize(jreader) as JObject;

                    string documentName  = metadata["d"]["Name"].ToString();
                    string fileExtension = documentName.Substring(documentName.LastIndexOf("."));


                    lockObj = CreateLockObject(Id, "isSupported");
                    bool isSupportedExt = false;
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        DeleteCachedDataInLockObject(previousLockObj);
                        isSupportedExt = isSupported(fileExtension);
                        if (isSupportedExt == false && fileExtension.ToLower() != ".pdf")
                        {
                            ReleaseLock(lockObj);
                            return;
                        }
                        ReleaseLock(lockObj);
                    }

                    string      media_src    = metadata["d"]["Binary"]["__mediaresource"]["media_src"].ToString();
                    string      content_type = metadata["d"]["Binary"]["__mediaresource"]["content_type"].ToString();
                    IEnumerable actions      = metadata["d"]["__metadata"]["actions"].Children();
                    Hashtable   actionsMap   = new Hashtable();
                    foreach (object action in actions)
                    {
                        JObject jObj = action as JObject;
                        actionsMap.Add(jObj["name"].ToString(), jObj);
                    }
                    lockObj = CreateLockObject(Id, "DownloadFile");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        if (File.Exists(TemporaryDirPath + Id + fileExtension) == false)
                        {
                            client.DownloadFile(Repo + media_src, TemporaryDirPath + Id + fileExtension);
                        }
                        ReleaseLock(lockObj);
                    }
                    lockObj = CreateLockObject(Id, "ConvertToPdf");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        if (File.Exists(TemporaryDirPath + Id + ".pdf") == false)
                        {
                            ConvertToPdf(TemporaryDirPath + Id + fileExtension, TemporaryDirPath + Id + ".pdf");
                        }
                        ReleaseLock(lockObj);
                    }
                    lockObj = CreateLockObject(Id, "DeleteDownloadedFile");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        if (File.Exists(TemporaryDirPath + Id + fileExtension) && fileExtension.ToLower() != ".pdf")
                        {
                            File.Delete(TemporaryDirPath + Id + fileExtension);
                        }
                        ReleaseLock(lockObj);
                    }

                    Size previewImageSize          = new Size();
                    Size previewImageThumbnailSize = new Size();
                    if (fileExtension.ToLower() == ".pdf")
                    {
                        previewImageSize.Width           = 600;
                        previewImageSize.Height          = 850;
                        previewImageThumbnailSize.Width  = 200;
                        previewImageThumbnailSize.Height = 200;
                    }
                    else
                    {
                        lockObj = CreateLockObject(Id, "");
                        if (IsLocked(lockObj))
                        {
                            return;
                        }
                        else
                        {
                            DoLock(lockObj);
                            FileService.FileServiceClient OOClient = new FileService.FileServiceClient();
                            previewImageSize          = OOClient.GetSuggestImageSize(fileExtension, false);
                            previewImageThumbnailSize = OOClient.GetSuggestImageSize(fileExtension, true);
                            OOClient.Close();
                            ReleaseLock(lockObj);
                        }
                    }
                    int numOfPages = 0;
                    lockObj = CreateLockObject(Id, "ConvertPdfToImages");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        if (File.Exists(TemporaryDirPath + Id + ".png") == false && File.Exists(TemporaryDirPath + Id + ".1.png") == false)
                        {
                            numOfPages = ConvertPdfToImages(TemporaryDirPath + Id + ".pdf", TemporaryDirPath, Id + ".{0}", previewImageSize.Width, previewImageSize.Height, false);
                        }
                        ReleaseLock(lockObj);
                    }
                    lockObj = CreateLockObject(Id, "ConvertPdfToThumbnailImages");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        if (File.Exists(TemporaryDirPath + Id + ".thumbnail.png") == false && File.Exists(TemporaryDirPath + Id + ".thumbnail.1.png") == false)
                        {
                            numOfPages = ConvertPdfToImages(TemporaryDirPath + Id + ".pdf", TemporaryDirPath, Id + ".thumbnail.{0}", previewImageThumbnailSize.Width, previewImageThumbnailSize.Height, true);
                        }
                        ReleaseLock(lockObj);
                    }
                    lockObj = CreateLockObject(Id, "DeletePdfFile");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        if (File.Exists(TemporaryDirPath + Id + ".pdf"))
                        {
                            File.Delete(TemporaryDirPath + Id + ".pdf");
                        }
                        ReleaseLock(lockObj);
                    }

                    string getPreviewFolderRequest = Repo + ((JObject)actionsMap["GetPreviewsFolder"])["target"].ToString();
                    //File.AppendAllText(TemporaryDirPath + file2, Environment.NewLine + getPreviewFolderRequest);

                    JObject p = null;
                    string  responseString = "";
                    p          = new JObject();
                    p["empty"] = true;
                    lockObj    = CreateLockObject(Id, "GetPreviewFolder");
                    if (IsLocked(lockObj))
                    {
                        return;
                    }
                    else
                    {
                        DoLock(lockObj);
                        responseString = GetCachedDataFromLockObject(lockObj);
                        if (responseString == null)
                        {
                            serializer.Serialize(jwriter, p);
                            responseString = client.UploadString(getPreviewFolderRequest, builder.ToString());
                            SaveCacheDataInLockObject(lockObj, responseString);
                            builder.Clear();
                        }
                        else
                        {
                            DeleteCachedDataInLockObject(lockObj);
                        }
                        previousLockObj = lockObj;
                        ReleaseLock(lockObj);
                    }

                    jreader = new JsonTextReader(new StringReader(responseString));
                    //DeleteCachedDataInLockObject(lockObj);
                    JObject jsonResponse      = serializer.Deserialize(jreader) as JObject;
                    string  previewFolderId   = jsonResponse["Id"].ToString();
                    string  previewFolderPath = jsonResponse["Path"].ToString();

                    string uploadRequest          = Repo + ODATA_SERVICE_PATH + string.Format(GET_CONTENT_BY_ID_REQUEST_FORMAT, previewFolderId) + "/Upload";
                    NameValueCollection arguments = new NameValueCollection();
                    string setPageCountRequest    = Repo + ((JObject)actionsMap["SetPageCount"])["target"].ToString();

                    /*lockObj = CreateLockObject(Id, "SetPageCount");
                     * if (IsLocked(lockObj))
                     * {
                     *  return;
                     * }
                     * else
                     * {
                     *  DoLock(lockObj);
                     *  DeleteCachedDataInLockObject(previousLockObj);
                     *  p = new JObject();
                     *  p["pageCount"] = 0;
                     *  arguments.Add("pageCount", 0.ToString());
                     *  serializer.Serialize(jwriter, p);
                     *  client.UploadString(setPageCountRequest, builder.ToString());
                     *  //client.UploadValues(setPageCountRequest, arguments);
                     *  arguments.Clear();
                     *  builder.Clear();
                     *  ReleaseLock(lockObj);
                     * }*/
                    for (int i = 0; i < numOfPages; i++)
                    {
                        string uploadFileName    = string.Format(PREVIEW_IMAGE_FORMAT, (i + 1).ToString());
                        string fullInputFilePath = TemporaryDirPath + string.Format(TEMP_PREVIEW_IMAGE_FORMAT, Id, (i + 1));

                        lockObj = CreateLockObject(Id, "UploadPreviewImage" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            DeleteCachedDataInLockObject(previousLockObj);
                            if (File.Exists(fullInputFilePath))
                            {
                                UploadFile(uploadRequest, fullInputFilePath, uploadFileName, "PreviewImage");
                            }
                            ReleaseLock(lockObj);
                        }
                        lockObj = CreateLockObject(Id, "DeletePreviewImage" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            if (File.Exists(fullInputFilePath))
                            {
                                File.Delete(fullInputFilePath);
                            }
                            ReleaseLock(lockObj);
                        }
                        string uploadThumbnailFileName    = string.Format(PREVIEW_IMAGE_THUMBNAIL_FORMAT, (i + 1).ToString());
                        string fullInputThumbnailFilePath = TemporaryDirPath + string.Format(TEMP_PREVIEW_IMAGE_THUMBNAIL_FORMAT, Id, (i + 1));

                        lockObj = CreateLockObject(Id, "UploadPreviewImageThumbnail" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            if (File.Exists(fullInputThumbnailFilePath))
                            {
                                UploadFile(uploadRequest, fullInputThumbnailFilePath, uploadThumbnailFileName, "PreviewImage");
                            }
                            ReleaseLock(lockObj);
                        }
                        lockObj = CreateLockObject(Id, "DeletePreviewImageThumbnail" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            if (File.Exists(fullInputThumbnailFilePath))
                            {
                                File.Delete(fullInputThumbnailFilePath);
                            }
                            ReleaseLock(lockObj);
                        }
                        string path = previewFolderPath;
                        if (path.EndsWith("/"))
                        {
                            path = path.Substring(0, path.Length - 1);
                        }
                        string setInitialPreviewPropertiesRequest = Repo + path + "('" + uploadFileName + "')/SetInitialPreviewProperties";
                        lockObj = CreateLockObject(Id, "SetInitialPreviewProperties" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            p = new JObject();
                            serializer.Serialize(jwriter, p);
                            client.UploadString(setInitialPreviewPropertiesRequest, builder.ToString());
                            builder.Clear();
                            ReleaseLock(lockObj);
                        }

                        setInitialPreviewPropertiesRequest = Repo + path + "('" + uploadThumbnailFileName + "')/SetInitialPreviewProperties";
                        lockObj = CreateLockObject(Id, "SetInitialPreviewPropertiesThumbnail" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            p = new JObject();
                            serializer.Serialize(jwriter, p);
                            client.UploadString(setInitialPreviewPropertiesRequest, builder.ToString());
                            builder.Clear();
                            ReleaseLock(lockObj);
                        }

                        lockObj = CreateLockObject(Id, "SetPageCount" + (i + 1));
                        if (IsLocked(lockObj))
                        {
                            continue;
                        }
                        else
                        {
                            DoLock(lockObj);
                            DeleteCachedDataInLockObject(previousLockObj);
                            p = new JObject();
                            p["pageCount"] = (i + 1);
                            arguments.Add("pageCount", (i + 1).ToString());
                            serializer.Serialize(jwriter, p);
                            client.UploadString(setPageCountRequest, builder.ToString());
                            //client.UploadValues(setPageCountRequest, arguments);
                            arguments.Clear();
                            builder.Clear();
                            ReleaseLock(lockObj);
                        }
                    }

                    /*string setPreviewStatusRequest = Repo + ((JObject)actionsMap["SetPreviewStatus"])["target"].ToString();
                     *
                     * lockObj = CreateLockObject(Id, "SetPreviewStatus");
                     * if (IsLocked(lockObj))
                     *  return;
                     * else
                     * {
                     *  DoLock(lockObj);
                     *  p = new JObject();
                     *  p["status"] = "Ready";
                     *  serializer.Serialize(jwriter, p);
                     *  arguments.Add("status", "Ready");
                     *  client.UploadString(setPreviewStatusRequest, builder.ToString().Replace("\"Ready\"", "Ready"));
                     *  //client.UploadValues(setPreviewStatusRequest, arguments);
                     *  arguments.Clear();
                     *  builder.Clear();
                     *  ReleaseLock(lockObj);
                     * }*/
                }
                catch (Exception ex)
                {
                    if (string.IsNullOrEmpty(lockObj) == false)
                    {
                        DeleteCachedDataInLockObject(lockObj);
                    }
                    SaveExceptionForLockObject(lockObj, ex);
                    ReleaseLock(lockObj);
                }
            }

            /*for (var i = 0; i < 10; i++)
             * {
             *  //do stuff...
             *  Console.WriteLine("Progress: {0}%", i * 10);
             * }*/

            Console.WriteLine("Progress: Execution ended.");
        }
Пример #3
0
 public static void ConvertToPdf(string inputFile, string outputFile)
 {
     FileService.FileServiceClient client = new FileService.FileServiceClient();
     client.ConvertToPDF(inputFile, outputFile);
     client.Close();
 }