-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pdf support in root of project folder name, for slides
- Loading branch information
Showing
7 changed files
with
312 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
utils/google/googleDrive/resources/createPDFResource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { drive_v3 } from "googleapis"; | ||
import { searchForFileByChildResourceId } from "../searchForFolder"; | ||
import { determineCategoryType } from "../determineCategoryType"; | ||
import { convertToStr } from "../../../general/convertToStr"; | ||
import { getDocuments } from "../../../crudRestApiMethods/getMethod"; | ||
import { ProjectDocument } from "../../../../app/lib/restAPI/resources/utils/types/projectTypes"; | ||
import { corsHeaders } from "../../../../app/lib/restAPI/resources/utils/corsLambda"; | ||
import { uploadImgToS3 } from "../../../general/s3Actions"; | ||
import { updateDocument } from "../../../crudRestApiMethods/postMethod"; | ||
import { marshall } from "@aws-sdk/util-dynamodb"; | ||
|
||
export type CreatePDFResourceProps = { | ||
restApiUrl: string; | ||
apiKey: string; | ||
bucketName: string; | ||
drive: drive_v3.Drive; | ||
resourceId: string; | ||
}; | ||
export type UploadProjectDocumentProps = { | ||
restApiUrl: string; | ||
apiKey: string; | ||
bucketName: string; | ||
fileBuffer: Buffer; | ||
key: string; | ||
resourceId: string; | ||
parentName: string | undefined | null; | ||
result: { | ||
file: drive_v3.Schema$File; | ||
fileBlob: Blob | null; | ||
parents: null | drive_v3.Schema$File; | ||
}; | ||
}; | ||
|
||
const topMostDirectoryFolderName = process.env.GOOGLE_DRIVE_FOLDER_NAME; | ||
const uploadPDFResourceItems = async ({ | ||
restApiUrl, | ||
apiKey, | ||
fileKey, | ||
resourceId, | ||
bucketName, | ||
fileBuffer, | ||
addedRoute, | ||
data, | ||
result, | ||
}: Pick< | ||
UploadProjectDocumentProps, | ||
| "restApiUrl" | ||
| "apiKey" | ||
| "fileBuffer" | ||
| "bucketName" | ||
| "resourceId" | ||
| "result" | ||
> & { | ||
data: ProjectDocument; | ||
fileKey: string; | ||
addedRoute: string; | ||
}) => { | ||
const updateResult = updateDocument({ | ||
restApiUrl, | ||
apiKey, | ||
data: { | ||
key: marshall(data.pk, { | ||
convertClassInstanceToMap: true, | ||
removeUndefinedValues: true, | ||
}), | ||
slidesURL: fileKey, | ||
slidesGoogleResourceId: resourceId, | ||
slidesFileName: result.file.name, | ||
}, | ||
addedRoute: addedRoute, | ||
}); | ||
|
||
//this actually uploads a file to s3, not just images | ||
const uploadFile = uploadImgToS3({ | ||
bucketName, | ||
key: fileKey, | ||
body: fileBuffer, | ||
}); | ||
const promiseArr = await Promise.all([uploadFile, updateResult]); | ||
return promiseArr; | ||
}; | ||
const uploadToProjects = async ({ | ||
restApiUrl, | ||
apiKey, | ||
parentName, | ||
bucketName, | ||
fileBuffer, | ||
key, | ||
resourceId, | ||
result, | ||
}: UploadProjectDocumentProps) => { | ||
const getDocResultsPromise = getDocuments({ | ||
restApiUrl, | ||
apiKey, | ||
addedRoute: "projects", | ||
params: { | ||
query: JSON.stringify({ | ||
recordType: "projects", | ||
projectName: parentName, | ||
}), | ||
}, | ||
}); | ||
const [docResults] = await Promise.all([getDocResultsPromise]); | ||
//continue if a project doc | ||
const docItems = docResults?.data?.result?.Items; | ||
if (!docItems || docItems.length <= 0) | ||
return { | ||
statusCode: 200, | ||
headers: corsHeaders, | ||
message: `No relevant project found with the following name: ${parentName}`, | ||
}; | ||
const doc = docItems[0] as ProjectDocument; | ||
return await uploadPDFResourceItems({ | ||
restApiUrl, | ||
apiKey, | ||
bucketName, | ||
fileBuffer: fileBuffer, | ||
addedRoute: "projects", | ||
fileKey: key, | ||
resourceId, | ||
data: doc, | ||
result, | ||
}); | ||
}; | ||
|
||
export const createPDFResource = async ({ | ||
restApiUrl, | ||
apiKey, | ||
bucketName, | ||
drive, | ||
resourceId, | ||
}: CreatePDFResourceProps) => { | ||
const result = await searchForFileByChildResourceId(drive, resourceId, true); | ||
const parentName = result.parents?.name; | ||
const key = `${parentName}/${resourceId}/pdf`; | ||
const keyWithType = `${key}.${result.file.fileExtension}`; | ||
const fileBuffer = result.fileBlob | ||
? Buffer.from(await result.fileBlob.arrayBuffer()) | ||
: null; | ||
if (!fileBuffer) return; | ||
const category = await determineCategoryType({ | ||
drive, | ||
fileData: result, | ||
topMostDirectoryFolderName: convertToStr(topMostDirectoryFolderName), | ||
}); | ||
let results: any; | ||
switch (category) { | ||
case "projects": | ||
results = await uploadToProjects({ | ||
restApiUrl, | ||
apiKey, | ||
parentName, | ||
bucketName, | ||
fileBuffer, | ||
key: keyWithType, | ||
resourceId, | ||
result, | ||
}); | ||
break; | ||
default: | ||
return; | ||
} | ||
return results.map((result: any) => { | ||
const newObj = { ...result } as any; | ||
if (newObj["$metadata"]) delete newObj["$metadata"]; | ||
if (newObj["$fault"]) delete newObj["$fault"]; | ||
if (newObj.headers) delete newObj["headers"]; | ||
if (newObj.request) delete newObj["request"]; | ||
if (newObj.config) delete newObj["config"]; | ||
if (newObj.request) delete newObj["request"]; | ||
return newObj; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.