2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-02-24 11:32:28 -06:00
import { prisma } from "@/lib/api/db" ;
2023-05-27 11:29:39 -05:00
import { LinkIncludingCollectionAndTags } from "@/types/global" ;
2023-03-08 15:31:24 -06:00
import getTitle from "../../getTitle" ;
import archive from "../../archive" ;
2023-03-10 13:25:33 -06:00
import { Link , UsersAndCollections } from "@prisma/client" ;
2023-03-08 15:31:24 -06:00
import AES from "crypto-js/aes" ;
2023-04-26 15:40:48 -05:00
import getPermission from "@/lib/api/getPermission" ;
2023-05-20 14:25:00 -05:00
import { existsSync , mkdirSync } from "fs" ;
2023-02-24 11:32:28 -06:00
2023-05-27 11:29:39 -05:00
export default async function (
link : LinkIncludingCollectionAndTags ,
userId : number
) {
2023-03-28 02:31:50 -05:00
link . collection . name = link . collection . name . trim ( ) ;
2023-02-24 11:32:28 -06:00
if ( ! link . name ) {
2023-03-28 02:31:50 -05:00
return { response : "Please enter a valid name for the link." , status : 401 } ;
} else if ( ! link . collection . name ) {
return { response : "Please enter a valid collection name." , status : 401 } ;
2023-02-24 11:32:28 -06:00
}
2023-05-27 11:29:39 -05:00
if ( link . collection . id ) {
2023-04-26 15:40:48 -05:00
const collectionIsAccessible = await getPermission (
2023-03-28 02:31:50 -05:00
userId ,
link . collection . id
) ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
const memberHasAccess = collectionIsAccessible ? . members . some (
( e : UsersAndCollections ) = > e . userId === userId && e . canCreate
) ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
if ( ! ( collectionIsAccessible ? . ownerId === userId || memberHasAccess ) )
return { response : "Collection is not accessible." , status : 401 } ;
} else {
link . collection . ownerId = userId ;
2023-02-24 11:32:28 -06:00
}
2023-03-08 15:31:24 -06:00
const title = await getTitle ( link . url ) ;
2023-02-24 11:32:28 -06:00
2023-03-08 15:31:24 -06:00
const newLink : Link = await prisma . link . create ( {
2023-02-24 11:32:28 -06:00
data : {
name : link.name ,
2023-03-05 15:03:20 -06:00
url : link.url ,
2023-02-24 11:32:28 -06:00
collection : {
2023-03-28 02:31:50 -05:00
connectOrCreate : {
where : {
name_ownerId : {
ownerId : link.collection.ownerId ,
name : link.collection.name ,
} ,
} ,
create : {
name : link.collection.name ,
ownerId : userId ,
} ,
2023-02-24 11:32:28 -06:00
} ,
} ,
tags : {
2023-03-28 02:31:50 -05:00
connectOrCreate : link.tags.map ( ( tag ) = > ( {
2023-02-24 11:32:28 -06:00
where : {
2023-03-28 02:31:50 -05:00
name_ownerId : {
name : tag.name ,
ownerId : link.collection.ownerId ,
2023-02-24 11:32:28 -06:00
} ,
} ,
create : {
2023-03-28 02:31:50 -05:00
name : tag.name ,
owner : {
2023-02-24 11:32:28 -06:00
connect : {
2023-03-28 02:31:50 -05:00
id : link.collection.ownerId ,
2023-02-24 11:32:28 -06:00
} ,
} ,
} ,
} ) ) ,
} ,
2023-03-08 15:31:24 -06:00
title ,
screenshotPath : "" ,
pdfPath : "" ,
2023-02-24 11:32:28 -06:00
} ,
2023-03-08 15:31:24 -06:00
} ) ;
const AES_SECRET = process . env . AES_SECRET as string ;
2023-03-25 09:17:34 -05:00
const screenshotHashedPath = AES . encrypt (
2023-03-08 15:31:24 -06:00
` data/archives/ ${ newLink . collectionId } / ${ newLink . id } .png ` ,
AES_SECRET
) . toString ( ) ;
const pdfHashedPath = AES . encrypt (
` data/archives/ ${ newLink . collectionId } / ${ newLink . id } .pdf ` ,
AES_SECRET
) . toString ( ) ;
2023-05-27 11:29:39 -05:00
const updatedLink = await prisma . link . update ( {
2023-03-08 15:31:24 -06:00
where : { id : newLink.id } ,
2023-03-25 09:17:34 -05:00
data : { screenshotPath : screenshotHashedPath , pdfPath : pdfHashedPath } ,
2023-03-10 13:25:33 -06:00
include : { tags : true , collection : true } ,
2023-02-24 11:32:28 -06:00
} ) ;
2023-05-20 14:25:00 -05:00
const collectionPath = ` data/archives/ ${ updatedLink . collection . id } ` ;
if ( ! existsSync ( collectionPath ) )
mkdirSync ( collectionPath , { recursive : true } ) ;
2023-03-08 15:31:24 -06:00
archive ( updatedLink . url , updatedLink . collectionId , updatedLink . id ) ;
2023-03-28 02:31:50 -05:00
return { response : updatedLink , status : 200 } ;
2023-02-24 11:32:28 -06:00
}