/// <summary> /// tests moving a document in and out of folders /// </summary> [Test] public void ModelTestArbitraryDownload() { RequestSettings settings = new RequestSettings(this.ApplicationName, this.userName, this.passWord); // settings.PageSize = 15; DocumentsRequest r = new DocumentsRequest(settings); Feed <Document> feed = r.GetEverything(); foreach (Document d in feed.Entries) { Stream res = r.Download(d, null); Assert.IsNotNull(res, "The download stream should not be null"); } foreach (Document d in feed.Entries) { Stream res = r.Download(d, "pdf"); Assert.IsNotNull(res, "The download stream should not be null"); } }
/// <summary> /// tests document download /// </summary> [Test] public void ModelTestDocumentDownload() { RequestSettings settings = new RequestSettings(this.ApplicationName, this.userName, this.passWord); settings.AutoPaging = true; DocumentsRequest r = new DocumentsRequest(settings); // this returns the server default answer Feed <Document> feed = r.GetDocuments(); foreach (Document x in feed.Entries) { Assert.IsTrue(x != null, "We should have something"); Stream ret = r.Download(x, Document.DownloadType.pdf); ret.Close(); } }
/// <summary> /// tests document download /// </summary> [Test] public void ModelTestDocumentDownload() { RequestSettings settings = new RequestSettings(this.ApplicationName, this.userName, this.passWord); settings.AutoPaging = true; DocumentsRequest r = new DocumentsRequest(settings); // this returns the server default answer Feed<Document> feed = r.GetDocuments(); Document d = null; foreach (Document x in feed.Entries ) { Assert.IsTrue(x != null, "We should have something"); Stream ret = r.Download(x, Document.DownloadType.pdf); ret.Close(); } }
/// <summary> /// tests moving a document in and out of folders /// </summary> [Test] public void ModelTestArbitraryDownload() { RequestSettings settings = new RequestSettings(this.ApplicationName, this.userName, this.passWord); // settings.PageSize = 15; DocumentsRequest r = new DocumentsRequest(settings); Feed<Document> feed = r.GetEverything(); foreach (Document d in feed.Entries) { Stream res = r.Download(d, null); Assert.IsNotNull(res, "The download stream should not be null"); } foreach (Document d in feed.Entries) { Stream res = r.Download(d, "pdf"); Assert.IsNotNull(res, "The download stream should not be null"); } }
/// <summary> /// Exec backup (internal) /// </summary> private int ExecBackupSingleUser(string username) { DoFeedback(new string('-', 80)); DoFeedback("--- ExecBackupSingleUser - username="******" ---"); DoFeedback(new string('-', 80)); _lastException = null; _duplicatedDocNames = new List <string>(); // Setup credentials and connection DoFeedback("Setup connection & get doc list"); RequestSettings settings; if (_config.appsMode == false) { GDataCredentials credentials = new GDataCredentials(_config.userName, _config.password); settings = new RequestSettings("GDocBackup", credentials); settings.AutoPaging = true; settings.PageSize = 100; } else { settings = new RequestSettings("GDocBackup", _config.appsDomain, _config.appsOAuthSecret, username, _config.appsDomain); settings.AutoPaging = true; settings.PageSize = 100; //settings.Maximum = 10000; } DocumentsRequest request = new DocumentsRequest(settings); if (_config.iwebproxy != null) { request.Proxy = _config.iwebproxy; } // Get doc list from GDocs Feed <Document> feed = request.GetEverything(); List <Document> docs = new List <Document>(); foreach (Document entry in feed.Entries) { docs.Add(entry); } // Search for duplicated doc names in the same folder _duplicatedDocNames = this.FindDuplicatedNames(docs); DoFeedback("Duplicated Doc Names [" + _duplicatedDocNames.Count + "]"); _duplicatedDocNames.ForEach(delegate(string s) { DoFeedback(" - " + s); }); // Builds/updates local folder structure if (_config.appsMode) { this.BuildFolders(null, docs, Path.Combine(_config.outDir, username)); } else { this.BuildFolders(null, docs, _config.outDir); } foreach (String k in _folderDict.Keys) { DoFeedbackDebug("FolderDict: " + k + " --> " + _folderDict[k]); } this.DumpAllDocInfo(docs); // Main Docs loop! int errorCount = 0; for (int i = 0; i < docs.Count; i++) { Document doc = docs[i]; DoFeedback("ITEM: " + doc.Title + " (" + doc.Type + ") [" + (i + 1).ToString() + "/" + docs.Count + "]", ((double)i) / docs.Count); Document.DownloadType[] downloadTypes = null; switch (doc.Type) { case Document.DocumentType.Document: downloadTypes = _config.docExpType; break; case Document.DocumentType.Presentation: downloadTypes = _config.presExpType; break; case Document.DocumentType.Spreadsheet: downloadTypes = _config.sprdExpType; break; case Document.DocumentType.PDF: downloadTypes = new Document.DownloadType[] { Document.DownloadType.pdf }; break; case Document.DocumentType.Drawing: downloadTypes = _config.drawExpType; break; case Document.DocumentType.Unknown: downloadTypes = new Document.DownloadType[] { Document.DownloadType.zip }; // download format not used! It's only a "place-holder". break; default: break; } if (downloadTypes != null) { int maxTentativi = 2; for (int tentativi = 0; tentativi < maxTentativi; tentativi++) { try { // * WorkAround for drawing * // Detect if drawing and then force downloadtype to pdf //bool isDrawing = doc.ResourceId.StartsWith("drawing:"); // drawing:14TBycKwlpXJ25N...... //if (isDrawing) // downloadTypes = new Document.DownloadType[] { Document.DownloadType.pdf }; // bool isDrawing = false; foreach (Document.DownloadType downloadtype in downloadTypes) { // Build local file path string outFolderPath; if (doc.ParentFolders.Count == 0) { outFolderPath = _config.appsMode ? Path.Combine(_config.outDir, username) : _config.outDir; } else { DoFeedback("Try to get folder from dict using key=[" + doc.ParentFolders[0] + "]"); outFolderPath = _folderDict[doc.ParentFolders[0]]; } string outFileFP = (doc.Type == Document.DocumentType.Unknown) ? Path.Combine(outFolderPath, this.RemoveInvalidChars(doc.Title, true)) : Path.Combine(outFolderPath, this.RemoveInvalidChars(doc.Title, false) + "." + ConvertDownloadTypeToFileExtension(downloadtype)); // Get current local file in infos FileInfo fi = new FileInfo(outFileFP); DateTime locFileDateTime = fi.LastWriteTime; DateTime gdocFileDateTime = doc.Updated; // Mono and/or Ubuntu (...linux) does not support milliseconds info when saving DataTime to FileInfo.LastWriteTime. So... I remove it! :) locFileDateTime = this.RemoveMilliseconds(locFileDateTime); gdocFileDateTime = this.RemoveMilliseconds(gdocFileDateTime); bool downloadDoc = (!fi.Exists || _config.downloadAll); if (_config.dateDiff.HasValue) { if (Math.Abs(locFileDateTime.Subtract(gdocFileDateTime).TotalSeconds) > _config.dateDiff.Value) { downloadDoc = true; } } else { if (locFileDateTime != gdocFileDateTime) { downloadDoc = true; } } if (downloadDoc) { DoFeedback("Start exporting " + doc.Title + "(Type=" + doc.Type + ") --> " + downloadtype.ToString()); Stream gdocStream = null; try { if (doc.Type == Document.DocumentType.Unknown) { String downloadUrl = doc.DocumentEntry.Content.Src.ToString(); Uri downloadUri = new Uri(downloadUrl); if (_config.appsMode) { // add xoauth_requestor_id to the doc url if not present if (!downloadUrl.Contains("xoauth_requestor_id=")) { downloadUri = new Uri(downloadUrl + "&xoauth_requestor_id=" + this.BuildDomainUserFullName(username)); } } gdocStream = request.Service.Query(downloadUri); } else if (doc.Type == Document.DocumentType.Document) { gdocStream = request.Download(doc, downloadtype.ToString()); } else if (doc.Type == Document.DocumentType.Spreadsheet) { gdocStream = request.Download(doc, downloadtype.ToString()); // WAS: downloadtype.ToString()); } else if (doc.Type == Document.DocumentType.Presentation) { gdocStream = request.Download(doc, downloadtype.ToString()); } else if (doc.Type == Document.DocumentType.Drawing) { gdocStream = request.Download(doc, downloadtype.ToString()); } else if (doc.Type != Document.DocumentType.PDF) { // *** ??? *** gdocStream = request.Download(doc, downloadtype); } else { // *** PDF *** if (_config.appsMode) { // add xoauth_requestor_id to the doc url if not present string url = doc.DocumentEntry.Content.Src.ToString(); if (!url.Contains("xoauth_requestor_id=")) { doc.DocumentEntry.Content.Src = new AtomUri(url + "&xoauth_requestor_id=" + this.BuildDomainUserFullName(username)); } } gdocStream = request.Download(doc, null); } using (FileStream outFile = new FileStream(outFileFP, FileMode.Create, FileAccess.Write)) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = gdocStream.Read(buffer, 0, buffer.Length)) > 0) { outFile.Write(buffer, 0, bytesRead); } outFile.Close(); } gdocStream.Close(); } finally { if (gdocStream != null) { gdocStream.Dispose(); } } new FileInfo(outFileFP).LastWriteTime = doc.Updated; DoFeedback("End exporting " + doc.Title + "(Type=" + doc.Type + ") --> " + downloadtype.ToString()); // ------------------------------------------------------------------------------------------------------------------------ // Workaround for Issue 100 - http://code.google.com/p/gdocbackup/issues/detail?id=100 if (doc.Type == Document.DocumentType.Presentation) { bool isPPTX = false; using (FileStream presentationFile = new FileStream(outFileFP, FileMode.Open, FileAccess.Read)) { int byte1 = presentationFile.ReadByte(); int byte2 = presentationFile.ReadByte(); isPPTX = (byte1 == 80 && byte2 == 75); // 80 75 = "PK" (pptx is a zip. Every zip starts with "PK" presentationFile.Close(); } if (!isPPTX) { string newName = outFileFP.Remove(outFileFP.Length - 1); File.Delete(newName); File.Move(outFileFP, newName); DoFeedback("Presentation API bug: renaming output file [" + newName + "]"); } } // ------------------------------------------------------------------------------------------------------------------------ } else { DoFeedback("Skipped doc: " + doc.Title); } // Send Feedback DoFeedback(new FeedbackObject( (_config.appsMode ? username + "#" + doc.Title : doc.Title), doc.Type.ToString(), (doc.Type == Document.DocumentType.Unknown) ? "BIN" : downloadtype.ToString(), downloadDoc ? "BCKUP" : "SKIP", "", locFileDateTime, gdocFileDateTime)); tentativi = maxTentativi; } } catch (Exception ex) { if (tentativi == maxTentativi - 1) { errorCount++; DoFeedback("DOC-ERROR: " + ex.ToString()); DoFeedback(new FeedbackObject( (_config.appsMode ? username + "#" + doc.Title : doc.Title), doc.Type.ToString(), "", "ERROR", "", null, null)); } else { DoFeedback("DOC-ERROR: (attempt " + tentativi + ") " + ex.ToString()); } } } } else { if (doc.Type != Document.DocumentType.Folder) { DoFeedback(new FeedbackObject(doc.Title, doc.Type.ToString(), "", "NONE", "", null, null)); } } } return(errorCount); }